Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from collections import deque
- from numpy import zeros
- def fetch_x(
- seed = deque([1,1,1], maxlen=3),
- a_1 = 0, a_2 = 63308, a_3 = -183326, m_1 = 2**31 - 1
- ):
- """
- x_n = (a_1 x_{n-1} + a_2 x_{n-2} + a_3 x_{n-3})
- with coefficients a_1 = 0, a_2 = 63308, a_3 = -183326,
- and moduli m_1 = 2**31-1 = 2147483647
- This function will remember the values of 3 previous calls
- We need to remember 3 last entries, add new ones and discard old,
- so let's use the seed deque.
- """
- def xguts():
- nonlocal seed, a_1, a_2, a_3, m_1
- literal_guts = (a_1 * seed[0] + a_2 * seed[1] + a_3 * seed[2]) % m_1
- seed.appendleft(literal_guts) # add a new value for seed
- # and discard the oldest one
- return literal_guts
- return xguts
- def fetch_y(
- seed = deque([1,1,1], maxlen=3),
- b_1 = 86098, b_2 = 0, b_3 = -539608, m_2 = 2145483479
- ):
- """
- y_n = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) mod m_2
- with coefficients b_1 = 86098, b_2 = 0, b_3 = -539608,
- and moduli m_2 = 2145483479. This function will remember the values
- of 3 previous calls in the seed deque
- """
- def yguts():
- nonlocal seed, b_1, b_2, b_3, m_2
- literal_guts = (b_1 * seed[0] + b_2 * seed[1] + b_3 * seed[2]) % m_2
- seed.appendleft(literal_guts)
- return literal_guts
- return yguts
- def french_rng(x, y):
- """ This is a combined multiple recursive generator by L’Ecuyer.
- Its sequence is,
- z_n = (x_n - y_n) mod m_1
- P. L’Ecuyer, “Combined Multiple Recursive Random Number Generators”,
- Operations Research, 44, 5 (1996), 816–822.
- """
- m_1 = 2**31 - 1
- def yet_another_gut():
- nonlocal x, y, m_1
- return (x() - y()) % m_1
- return yet_another_gut
- N = 6
- arr = zeros(N, dtype=int) # prepare the space filled with zeros
- rand = french_rng(x = fetch_x(deque([2,2,2], maxlen=3)), y = fetch_y())
- for i in range (N):
- arr[i] = rand()
- print (arr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement