JavaScript实现随机产生字符串的方法分享

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JavaScript实现随机产生字符串的方法分享

KjPrime   2022-11-30 我要评论

这个东西就是随机产生字符串的函数。

参数,代码里面有解释。

用途:当作产生随机密码来看使用吧,或者nodejs后端存储数据库的主键来使用吧。

/**
 * 这个是一个随机产生字符串的函数
 * @param {object} cfg 参数
    cfg = {
        split:  '',                 // 分割字符 
        splitNum: 0,                // 分隔个数
        num: 16,                    // 产生随机字符个数 默认16
        char: 'dAa',                // d数字 A大写的字符 a小写的字符  默认dAa
        append: '@*!+-=*&[()`?.]',  // 添加的其他额外字符,支持数组[]和字符串 默认@*!+-=*&[()`?.
        ignore: '',                 // 可以忽略的字符,支持数组[]和字符串 优先级最高 
    }
 * @returns 返回随机字符串
 */
const rand_str = (cfg) => {
    if(cfg === undefined) cfg = {}
    const getValue = (s, d) => {
        if([undefined, null].includes(s)) return d
        else return s
    }
    cfg = {
        split:  getValue(cfg.split, ''),    // 分割字符 
        splitNum: parseInt(cfg.splitNum) || 0,   // 分隔个数
        num: parseInt(cfg.num) || 16,             // 字符个数 默认16
        char: getValue(cfg.char, 'dAa') ,        // d数字 A大写的字符 a小写的字符
        append: getValue(cfg.append, '@*!+-=*&[()`?.') , // 支持数组[]和字符串 一般指特殊字符@*!+-=*&[()`?.
        ignore: getValue(cfg.ignore, '')                       // 支持数组[]和字符串 优先级最高 
    }
    // console.log(cfg)
    let set = new Set()
    const getChars = (str) => {
        for(const s of str) set.add(s)
    }
    // 1、先取出append特殊字符的编码
    getChars(cfg.append)
    // 2、获取char中的字符集
    const number = "0123456789"
    const bigChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    const smallChars = "abcdefghijklmnopqrstuvwxyz"
    for(const s of cfg.char) {
        if(s === 'd') getChars(number)
        else if (s === 'A') getChars(bigChars)
        else if (s === 'a') getChars(smallChars)
        // 其他的字符 自动忽略
    }
    // 3. 排除ignore忽略的元素
    for(const s of cfg.ignore) set.delete(s)
    // 4. 生成数组
    const arr = Array.from(set)
    // console.log(arr)
    // 5. 打乱集合 
    const swap = (firstIndex, secondIdex) => {
        const t = arr[firstIndex]
        arr[firstIndex] = arr[secondIdex]
        arr[secondIdex] = t
    }
    const size = arr.length
    for(let i = 0; i < cfg.num; i++) swap(Math.floor(Math.random() * size), Math.floor(Math.random() * size))
    // 6、生成随机字符串
    let re = ""
    for(let i = 0; i < cfg.num; i++) {
        if(i % cfg.splitNum === 0 && i !== 0) re += cfg.split
        re += arr[Math.floor(Math.random() * size)]
    }
    return re
}




/**
 * 测试
 */
for(let i = 1; i< 10; i++) console.log(rand_str())
console.log('----------------------------')
const config = {
    split:  '',                 // 分割字符 
    splitNum: 0,                // 分隔个数
    num: 20,                    // 产生随机字符个数 默认16
    char: 'Aa',                // d数字 A大写的字符 a小写的字符  默认dAa
    append: '@*!+-=*&[()`?.]',  // 添加的其他额外字符,支持数组[]和字符串 默认@*!+-=*&[()`?.
    ignore: '@*!+-=*&[()`?.]',  // 可以忽略的字符,支持数组[]和字符串 优先级最高 
}
for(let i = 1; i< 10; i++) console.log(rand_str(config))

知识点补充

math.random()

math.random()方法返回一个伪随机浮点数,结果区间为[0, 1),在区间内近似均匀分布,可以使用别的方法缩放到所需的范围。它实现了选择初始值的随机数生成算法;使用者无法主动选择值或进行重置。

Math.random();
// 0.21446359414239313

Math.random 会提供给我们一个[0,1)之间的随机数,但是如果我们要[1,10]范围随机整数的话,可以使用以下三个函数:

  • math.round() 四舍五入
  • math.ceil() 向上取整
  • math.floor() 向下取整
Math.ceil(Math.random() * 10);
// 7

快速生成随机字符串

利用 toString,hex 代表进制,最大不能超过 36,36 = 10 + 26 个英文字母 hex 越大,字母占比越多

Math.random().toString(36).slice(2);

注意:

  • 该方式无法保证字符串长度一致
  • 当 Math.random()的结果是有限长度小数时,比如 0.5,0.55,会导致得到的结果不符合预期

测试

// 十万次
var a = Array.from(new Array(100000).keys());
var b = a.map((i) => Math.random().toString(36).slice(2));
new Set(Object.values(b.map((i) => i.length)));
// Set(8) {10, 11, 9, 12, 8, 13, 14, 7}

可以自己试一下,每次运行的结果可能是不同的,其原因是 Math.random()生成的数字保留的小数长度本身是不固定的

// 百万次
var a = Array.from(new Array(1000000).keys());
new Set(Object.values(a.map((i) => (Math.random() + "").length)));
// Set(14) {19, 18, 17, 20, 16, 21, 22, 15, 14, 13, 23, 11, 24, 12}

快速生成固定长度的随机字符串

/**
 * 生成指定长度的字符串
 * @param {Number} hex 代表进制,取值范围[2 - 36],最大不能超过 36, 数字越大字母占比越高,小于11为全数字
 * @param {Number} len 字符串长度
 */
function generateStr(hex, len) {
  if (hex < 2 || hex > 36) {
    throw new RangeError("hex argument must be between 2 and 36");
  }

  var res = Math.random().toString(hex).slice(2);
  var resLen = res.length;

  while (resLen < len) {
    res += Math.random().toString(hex).slice(2);
    resLen = res.length;
  }
  return res.substr(0, len);
}

测试

// 执行十万次,可以在50ms左右稳定获取长度为10的随机字符串
console.time("exec");
var a = Array.from(new Array(100000).keys());
console.log(new Set(a.map((i) => generateStr(22, 10).length)));
console.timeEnd("exec");
// Set(1) {10}
// exec: 49.966064453125 ms

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们