在JavaScript中生成一个随机的字母数字(大写,小写和数字)字符串来用作可能唯一的标识符的最短方法是什么?


当前回答

这是一个简单的代码来生成随机字符串字母。 看看这段代码是如何工作的。 去(lenthOfStringToPrint);-使用此函数生成最终字符串。

var letters = {
  1: ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"],
  2: ["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"]
},i,letter,final="";
random = (max,min) => {
  return Math.floor(Math.random()*(max-min+1)+min);
}
function go(length) {
  final="",letter="";
  for (i=1; i<=length; i++){
    letter = letters[random(0,3)][random(0,25)];
    final+=letter;
  }
  return final;
}

其他回答

使用lodash:

生物多样性功能(length) var chars =“不可能” 瓦尔pwd = _sampleSize (chars,长度正好| | 12)/ lodash v4:用_ sampleSize。 pwd归来加入(“”)。 的 文件写(createRandomString(8)。 <剧本剧本src = " https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js " > < / >

随机密钥生成器

keyLength参数是你想要的键的字符长度

function keyGen(keyLength) {
    var i, key = "", characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    var charactersLength = characters.length;

    for (i = 0; i < keyLength; i++) {
        key += characters.substr(Math.floor((Math.random() * charactersLength) + 1), 1);
    }

    return key;
}


keyGen(12)
"QEt9mYBiTpYD"

或者根据Jar Jar的建议,这是我在最近的一个项目中使用的方法(以克服长度限制):

var randomString = function (len, bits)
{
    bits = bits || 36;
    var outStr = "", newStr;
    while (outStr.length < len)
    {
        newStr = Math.random().toString(bits).slice(2);
        outStr += newStr.slice(0, Math.min(newStr.length, (len - outStr.length)));
    }
    return outStr.toUpperCase();
};

Use:

randomString(12, 16); // 12 hexadecimal characters
randomString(200); // 200 alphanumeric characters

这是一个简单的代码来生成随机字符串字母。 看看这段代码是如何工作的。 去(lenthOfStringToPrint);-使用此函数生成最终字符串。

var letters = {
  1: ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"],
  2: ["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"]
},i,letter,final="";
random = (max,min) => {
  return Math.floor(Math.random()*(max-min+1)+min);
}
function go(length) {
  final="",letter="";
  for (i=1; i<=length; i++){
    letter = letters[random(0,3)][random(0,25)];
    final+=letter;
  }
  return final;
}
function randomString(len) {
    var p = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    return [...Array(len)].reduce(a=>a+p[~~(Math.random()*p.length)],'');
}

简介:

创建一个我们想要的大小的数组(因为javascript中没有等价的range(len))。 对于数组中的每个元素:从p中随机选择一个字符并将其添加到字符串中 返回生成的字符串。

这里做一些解释:

[...阵列望远镜(len)]

Array(len)或new Array(len)创建一个指针未定义的数组。单行语句将更难实现。Spread语法方便地定义了指针(现在它们指向未定义的对象!)

.reduce (

在本例中,将数组缩减为单个字符串。reduce功能在大多数语言中都很常见,值得学习。

a = > a+...

我们用的是箭头函数。

A是累加器。在本例中,它是当我们完成时将返回的最终结果字符串(你知道它是一个字符串,因为reduce函数的第二个参数initialValue是一个空字符串:")。基本上就是:用p[~~(Math.random()*p.length)]转换数组中的每个元素,将结果附加到a字符串中,当你完成时给我一个。

p[…]

P是我们要从中选择的字符串。你可以像访问索引一样访问字符串中的字符(例如,"abcdefg"[3]给了我们"d")

~ ~ (math . random () * p.length)

Math.random()返回一个位于[0,1]之间的浮点数。Math.floor(Math.random()*max)是javascript中获取随机整数的事实标准。~是javascript中按位的NOT操作符。 ~~是Math的一种更短、更快、更有趣的表达方式。(这里有一些信息