如何用javascript编写20个字符的随机生成器 [英] How to write in javascript for 20-character random generator

查看:69
本文介绍了如何用javascript编写20个字符的随机生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(用Javascript编程)



写一个20字符的随机生成器,它使用最少的资源(文件大小,内存,CPU)。密钥字符串中允许的有效字符

是a-z,A-Z和0-9。

例如83FEdmhjKla4WiYd6uem

(Program in Javascript)

Write a 20-character random generator that uses the least resource (file size, memory, CPU). The valid characters
allowed in the key string are a-z, A-Z and 0-9.
e.g. 83FEdmhjKla4WiYd6uem

推荐答案

http://vitaln.com/ehalos/ js / RANGen.html [ ^ ] < br $> b $ b

http://www.frihost.com/forums /vt-40859.html [ ^ ]


如果你可以在javascript中创建GUID,只需这样做,并在必要时删除超支和花括号。我在google找到了这个:



if you can create GUIDs in javascript, just do that, and remove the hypens and curly braces if necessary. I found this with google:

function createUUID() 
{ 
    // http://www.ietf.org/rfc/rfc4122.txt     
    var s = []; 
    var hexDigits = "0123456789abcdef"; 
    for (var i = 0; i < 36; i++) 
    {    
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);     
    } 
    s[14] = "4";  
    // bits 12-15 of the time_hi_and_version field to 0010     
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  
    // bits 6-7 of the clock_seq_hi_and_reserved to 01     
    s[8] = s[13] = s[18] = s[23] = "-";      
    var uuid = s.join("");     
    return uuid; 
} 





您可以更改hexDigits的内容以包含更多字母字符,将for循环调整为仅生成20个字符,并删除添加连字符的函数底部的代码。



You can change the contents of hexDigits to include more alphabetic characters, adjust the for loop to only generate 20 characters, and remove the code at the bottom of the function that adds the hyphens.


推荐此链接

可以帮助你



http://codegolf.stackexchange .com / questions / 1360 / how-to-write-a-single-line-password-generator [ ^ ]


这篇关于如何用javascript编写20个字符的随机生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆