Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def xorshift128():
- '''xorshift
- https://ja.wikipedia.org/wiki/Xorshift
- '''
- x = 123456789
- y = 362436069
- z = 521288629
- w = 88675123
- def _random():
- nonlocal x, y, z, w
- t = x ^ ((x << 11) & 0xFFFFFFFF) # 32bit
- x, y, z = y, z, w
- w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))
- return w
- return _random
- def xorshift128plus():
- '''xorshift+
- https://en.wikipedia.org/wiki/Xorshift#xorshift+
- http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf
- '''
- s0 = 1
- s1 = 2
- def _random():
- nonlocal s0, s1
- x, y = s0, s1
- x = x ^ ((x << 23) & 0xFFFFFFFFFFFFFFFF) # 64bit
- x = (x ^ (x >> 17)) ^ (y ^ (y >> 26))
- s0, s1 = y, x
- return s0 + s1
- return _random
- def main():
- r = xorshift128plus()
- for i in range(10):
- print(r())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement