Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from collections import deque
  4. from numpy import zeros
  5.  
  6.  
  7. def fetch_x(
  8.     seed = deque([1,1,1], maxlen=3),
  9.     a_1 = 0, a_2 = 63308, a_3 = -183326, m_1 = 2**31 - 1
  10. ):
  11.     """
  12.        x_n = (a_1 x_{n-1} + a_2 x_{n-2} + a_3 x_{n-3})
  13.        with coefficients a_1 = 0, a_2 = 63308, a_3 = -183326,
  14.        and moduli m_1 = 2**31-1 = 2147483647
  15.        This function will remember the values of 3 previous calls
  16.        We need to remember 3 last entries, add new ones and discard old,
  17.        so let's use the seed deque.
  18.    """
  19.     def xguts():
  20.         nonlocal seed, a_1, a_2, a_3, m_1
  21.         literal_guts = (a_1 * seed[0] + a_2 * seed[1] + a_3 * seed[2]) % m_1
  22.         seed.appendleft(literal_guts)       # add a new value for seed
  23.                                             # and discard the oldest one
  24.         return literal_guts
  25.     return xguts
  26.  
  27. def fetch_y(
  28.     seed = deque([1,1,1], maxlen=3),
  29.     b_1 = 86098, b_2 = 0, b_3 = -539608, m_2 = 2145483479
  30. ):
  31.     """
  32.        y_n = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) mod m_2
  33.        with coefficients b_1 = 86098, b_2 = 0, b_3 = -539608,
  34.        and moduli m_2 = 2145483479. This function will remember the values
  35.        of 3 previous calls in the seed deque
  36.    """
  37.     def yguts():
  38.         nonlocal seed, b_1, b_2, b_3, m_2
  39.         literal_guts = (b_1 * seed[0] + b_2 * seed[1] + b_3 * seed[2]) % m_2
  40.         seed.appendleft(literal_guts)      
  41.         return literal_guts
  42.     return yguts
  43.  
  44. def french_rng(x, y):
  45.     """ This is a combined multiple recursive generator by L’Ecuyer.
  46.        Its sequence is,
  47.        z_n = (x_n - y_n) mod m_1
  48.        P. L’Ecuyer, “Combined Multiple Recursive Random Number Generators”,
  49.        Operations Research, 44, 5 (1996), 816–822.
  50.    """
  51.     m_1 = 2**31 - 1            
  52.     def yet_another_gut():
  53.         nonlocal x, y, m_1
  54.         return (x() - y()) % m_1
  55.     return yet_another_gut
  56.  
  57.  
  58. N = 6
  59.  
  60. arr = zeros(N, dtype=int)       # prepare the space filled with zeros
  61.  
  62. rand = french_rng(x = fetch_x(deque([2,2,2], maxlen=3)), y = fetch_y())            
  63.  
  64.  
  65. for i in range (N):
  66.     arr[i] = rand()
  67.  
  68. print (arr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement