アルゴリズムはxorshift。研究用途等、端末を問わず、再現付き乱数を発生したい場合に必要。

;(function(root){
 function entry(seed){
  let x = 90215247,y = 75933011,z = 19246193,w = seed||39706195,re=16856449
  ,a=(d)=>{return (d===0)?re:d }  //a is work function. if all value 0, dont work, so the escape.
  ,f=()=>{
    let t= a(x^(x << 11)); x =a(y); y=a(z); z=a(w);
    return w = (w^(w >>> 19)) ^ (t ^ (t >>> 8)); 
  }
  ,f2=(min,max)=>{return min + (Math.abs(f()) % (max + 1 - min))}
  ;
  return function(min,max){return (!isNaN(min)&&!isNaN(max))?f2(min,max):f()}
 }
 root.rseed=entry;
})(this);///*usage:*/ let seed=100,r=rseed(seed); Array.from({length:100}).map(d=>r(0,555)).map(d=>{console.log(d)})

usage

/*usage:*/ let seed=100,r=rseed(seed); Array.from({length:100}).map(d=>r(0,555)).map(d=>{console.log(d)})

精度チェック

let seed=7,r=rseed(seed); 
let ret=Array.from({length:1000000}).map(d=>r(0,100)).filter(d=>d===0);//per check
console.log(ret.length/1000000 *100,'%')
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.