Ruby's Louvre

每天学习一点点算法

导航

不使用Math.random实现随机数。

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))
console.log(rand(100))
console.log(rand(100))
console.log(rand(100))
console.log(rand(100))
//原理参看这里 http://www.zhihu.com/question/22818104

posted on 2014-09-02 16:42  司徒正美  阅读(4271)  评论(2编辑  收藏  举报