JS生成指定长度的随机数
1 /** 2 * 生成指定长度的UUID 3 * @param len 4 * @param radix 5 * @returns uuid 6 * eg: createUUID(8, 2) "01001010" createUUID(8, 10) "47473046" createUUID(8, 16) "098F4D35" 7 */
调用
var num = createUUID(6, 10); 8 function createUUID(len, radix) { 9 var chars = '0123456789'.split(''); 10 var uuid = [], i; 11 radix = radix || chars.length; 12 if(len){ 13 for (i = 0; i < len; i++){ 14 uuid[i] = chars[0 | Math.random()*radix]; 15 } 16 } 17 return '-'+uuid.join(''); 18 }