-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarsaglia.py
38 lines (33 loc) · 929 Bytes
/
marsaglia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
from collections import deque
def mwc(a, b, xs, c):
"""Multiply With Carry PRNG; see
http://en.wikipedia.org/wiki/Multiply-with-carry
Example:
>>> prng = mwc(7, 10, [1], 3)
>>> [next(prng) for _ in range(20)]
[1, 0, 1, 7, 9, 7, 5, 0, 4, 8, 8, 1, 3, 2, 6, 3, 5, 7, 2, 9]
"""
xs = deque(xs)
while True:
x_out = xs.popleft()
x_in = (a * x_out + c) % b
c = (a * x_out + c) / b
xs.append(x_in)
yield x_out
def xor128():
"""Xorshift PRNG; see http://en.wikipedia.org/wiki/Xorshift
Example:
>>> prng = xor128()
>>> [next(prng) for _ in range(4)]
[252977563114L, 646616338854L, 476657867818L, 294684809458L]
"""
x = 123456789
y = 362436069
z = 521288629
w = 88675123
while True:
t = x ^ (x << 11)
x, y, z = y, z, w
w = w ^ (w >> 19) ^ (t ^ (t >> 8))
yield w