github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/lib/utils/random.js (about) 1 'use strict'; 2 3 /* global crypto:true */ 4 var crypto = require('crypto'); 5 6 // This string has length 32, a power of 2, so the modulus doesn't introduce a 7 // bias. 8 var _randomStringChars = 'abcdefghijklmnopqrstuvwxyz012345'; 9 module.exports = { 10 string: function(length) { 11 var max = _randomStringChars.length; 12 var bytes = crypto.randomBytes(length); 13 var ret = []; 14 for (var i = 0; i < length; i++) { 15 ret.push(_randomStringChars.substr(bytes[i] % max, 1)); 16 } 17 return ret.join(''); 18 } 19 20 , number: function(max) { 21 return Math.floor(Math.random() * max); 22 } 23 24 , numberString: function(max) { 25 var t = ('' + (max - 1)).length; 26 var p = new Array(t + 1).join('0'); 27 return (p + this.number(max)).slice(-t); 28 } 29 };