我想要一个5个字符的字符串,由从集合[a-zA-Z0-9]中随机选取的字符组成。

用JavaScript实现这一点的最佳方法是什么?


当前回答

生成包含aA zZ和0-9字符集合的随机字符串。只需使用长度参数调用此函数。

所以要回答这个问题:generateRandomString(5)

generateRandomString(length){
    let result = "", seeds

    for(let i = 0; i < length - 1; i++){
        //Generate seeds array, that will be the bag from where randomly select generated char
        seeds = [
            Math.floor(Math.random() * 10) + 48,
            Math.floor(Math.random() * 25) + 65,
            Math.floor(Math.random() * 25) + 97
        ]
        
        //Choise randomly from seeds, convert to char and append to result
        result += String.fromCharCode(seeds[Math.floor(Math.random() * 3)])
    }

    return result
}

生成不带数字的字符串的版本:

generateRandomString(length){
    let result = "", seeds

    for(let i = 0; i < length - 1; i++){
        seeds = [
            Math.floor(Math.random() * 25) + 65,
            Math.floor(Math.random() * 25) + 97
        ]
        result += String.fromCharCode(seeds[Math.floor(Math.random() * 2)])
    }

    return result
}

其他回答

如果可以使用库,Chance.js可能会有所帮助:http://chancejs.com/#string

这不是一个完美的解决方案,但它应该奏效。如果出现任何错误,请增加Uint8Array()构造函数中给定的值。此方法的优点是它使用getRandomValues()方法生成加密的强随机值。

var array=新Uint8Array(20);crypto.getRandomValues(数组);var arrayEncoded=btoa(String.fromCharCode(…array)).split(“”);var arrayFiltered=arrayEncoded.filter(值=>{开关(值){大小写“+”:return false;大小写“/”:return false;case“=”:return false;违约:返回true;}});var password=arrayFiltered.slice(0,5).join('');console.log(密码);

紧凑型

var array=新Uint8Array(20);crypto.getRandomValues(数组);var password=btoa(String.fromCharCode(…array)).split(“”).filter(值=>{回来['+','/','=']includes(value);}).切片(0,5).连接('');console.log(密码);

从字符a-Za-z0-9中随机化字符串的另一种好方法:

function randomString(length) {
    if ( length <= 0 ) return "";
    var getChunk = function(){
        var i, //index iterator
            rand = Math.random()*10e16, //execute random once
            bin = rand.toString(2).substr(2,10), //random binary sequence
            lcase = (rand.toString(36)+"0000000000").substr(0,10), //lower case random string
            ucase = lcase.toUpperCase(), //upper case random string
            a = [lcase,ucase], //position them in an array in index 0 and 1
            str = ""; //the chunk string
        b = rand.toString(2).substr(2,10);
        for ( i=0; i<10; i++ )
            str += a[bin[i]][i]; //gets the next character, depends on the bit in the same position as the character - that way it will decide what case to put next
        return str;
    },
    str = ""; //the result string
    while ( str.length < length  )
        str += getChunk();
    str = str.substr(0,length);
    return str;
}

回答“我需要随机字符串”问题(无论用什么语言)的问题是,实际上每个解决方案都使用有缺陷的字符串长度的主要规范。这些问题本身很少揭示为什么需要随机字符串,但我想挑战一下,你很少需要长度为8的随机字符串。您总是需要一些唯一的字符串,例如,用作某些目的的标识符。

有两种主要的方法可以获得严格唯一的字符串:确定性(这不是随机的)和存储/比较(这很麻烦)。我们该怎么做?我们放弃了幽灵。我们改为概率唯一性。也就是说,我们接受字符串不唯一的风险(无论多么小)。这就是理解碰撞概率和熵有帮助的地方。

因此,我将把不变的需求重新表述为需要一些字符串,但重复的风险很小。作为一个具体的例子,假设您希望生成500万个ID。您不希望存储和比较每个新字符串,并且希望它们是随机的,因此您接受一些重复的风险。例如,假设重复的风险小于一万亿分之一。那么你需要多长的绳子?嗯,这个问题没有具体说明,因为它取决于使用的字符。但更重要的是,这是错误的。您需要的是字符串熵的规范,而不是字符串的长度。熵可以与一些字符串中重复的概率直接相关。字符串长度不能。

这就是像EntropyString这样的库可以提供帮助的地方。要使用熵字符串在500万个字符串中生成重复概率小于1万亿的随机ID,请执行以下操作:

import {Random, Entropy} from 'entropy-string'

const random = new Random()
const bits = Entropy.bits(5e6, 1e12)

const string = random.string(bits)

“44hTNghjNHGGRHqH9”

熵字符串默认使用32个字符的字符集。还有其他预定义的字符集,您也可以指定自己的字符。例如,生成具有与上述相同熵但使用十六进制字符的ID:

import {Random, Entropy, charSet16} from './entropy-string'

const random = new Random(charSet16)
const bits = Entropy.bits(5e6, 1e12)

const string = random.string(bits)

“27b33372代码513715481f”

请注意,由于所使用的字符集中的字符总数不同,字符串长度不同。在指定数量的潜在字符串中重复的风险是相同的。字符串长度不是。最重要的是,重复的风险和字符串的潜在数量是明确的。不再猜测字符串长度。

这样怎么样:Date.now().toString(36)不是很随意,但每次调用它时都很短且非常独特。