别再用JS里面的Math.random()方法生成随机数

网上常能见到的一段 JS 随机数生成算法如下,为什么用 9301, 49297, 233280 这三个数字做基数?

见到这个随机数生成算法好几次了,乍看有点鸡肋本来用Math.random()就可以的事。想不清楚为什么他要用9301,49297,233280这三个数字?其中有道理吗?还是仅是随意选的三个数?但是这个组合貌似流传很广。好多小网站源码里都见到过。
 

 

var rand = (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(rand(100))

console.log(rand(100))

console.log(rand(100))

原理参看这里 http://www.zhihu.com/question/22818104


Google了一下这3个数字,一些说法也是人云亦云没有找到合理的解释。

猜你喜欢

转载自blog.csdn.net/corey_mengxiaodong/article/details/81067076