Ccmmutty logo
Commutty IT
1 min read

XorShift

https://cdn.magicode.io/media/notebox/water-funnel-gb90320d5c_1280.jpg
xorshiftでシード付きで疑似乱数を返す処理を書きました。メルセンヌツイスタが信用できないときにご利用ください。
xorshift=(seed = 114514)->
  x = 123456789
  y = 362436069
  z = 521288629
  w = seed
  
  loop
    t = x ^ (x << 11)
    x = y
    y = z
    z = w
    yield w = (w ^ (w >>> 19)) ^ (t ^ (t >>> 8))



xs1 = xorshift()
console.log [0...10].map -> xs1.next().value

xs2 = xorshift(141421356)
console.log [0...10].map -> xs2.next().value
[ -638850782,
  505002631,
  -1873222302,
  -1649532140,
  462056314,
  -929814770,
  -1615808373,
  1944755946,
  997456470,
  1387660897 ]
[ -779633583,
  376827641,
  -1741377519,
  -10019664,
  334385721,
  -1437240165,
  -1747128840,
  1352261935,
  830934687,
  813127064 ]

Discussion

コメントにはログインが必要です。