您的位置:首页 > Web前端 > JavaScript

js的随机数生成器,不再使用Math.random

2016-05-18 14:29 555 查看
先上代码:

var randomNum = (function(){

var today = new Date();

var seed = today.getTime();

function rnd(){

seed = ( seed * 9301 + 49297 ) % 233280;

return seed / ( 233280.0 );

};

return function rand(number){

return Math.ceil(rnd(seed) * number);

};

})();

测试下:

console.log(randomNum(100))

console.log(randomNum(100))

console.log(randomNum(100))

为神马会使用9301,49297和233280这几个数字?原理见这里 http://www.zhihu.com/question/22818104
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: