如何生成&组随机十六进制数?

时间:2016-06-18 15:46:19

标签: javascript jquery regex

使用十六进制生成一些随机字母和数字的最简单方法是什么?

例如:095904e8-121098-fb330c52-9ae4-4a44-a31d-b80f2c680ca0

1 个答案:

答案 0 :(得分:2)

没有正则表达式的js

function randomString(len) {
  len = len || 32;
  var $chars = 'abcdefhiklmnorstuvwxz0123456789';
  var maxPos = $chars.length;
  var pwd = '';
  for (i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}
document.write(randomString(8)+'-'+randomString(6)+'-'+randomString(8)+'-'+randomString(4)+'-'+randomString(4)+'-'+randomString(4)+'-'+randomString(12));