Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class LCG(object):
  4.  
  5. UZERO: np.uint32 = np.uint32(0)
  6. UONE : np.uint32 = np.uint32(1)
  7.  
  8. def __init__(self, seed: np.uint32, a: np.uint32, c: np.uint32) -> None:
  9. self._seed: np.uint32 = np.uint32(seed)
  10. self._a : np.uint32 = np.uint32(a)
  11. self._c : np.uint32 = np.uint32(c)
  12.  
  13. def next(self) -> np.uint32:
  14. self._seed = self._a * self._seed + self._c
  15. return self._seed
  16.  
  17. def seed(self) -> np.uint32:
  18. return self._seed
  19.  
  20. def set_seed(self, seed: np.uint32) -> np.uint32:
  21. self._seed = seed
  22.  
  23. def skip(self, ns: np.int32) -> None:
  24. """
  25. Signed argument - skip forward as well as backward
  26.  
  27. The algorithm here to determine the parameters used to skip ahead is
  28. described in the paper F. Brown, "Random Number Generation with Arbitrary Stride,"
  29. Trans. Am. Nucl. Soc. (Nov. 1994). This algorithm is able to skip ahead in
  30. O(log2(N)) operations instead of O(N). It computes parameters
  31. A and C which can then be used to find x_N = A*x_0 + C mod 2^M.
  32. """
  33.  
  34. nskip: np.uint32 = np.uint32(ns)
  35.  
  36. a: np.uint32 = self._a
  37. c: np.uint32 = self._c
  38.  
  39. a_next: np.uint32 = LCG.UONE
  40. c_next: np.uint32 = LCG.UZERO
  41.  
  42. while nskip > LCG.UZERO:
  43. if (nskip & LCG.UONE) != LCG.UZERO:
  44. a_next = a_next * a
  45. c_next = c_next * a + c
  46.  
  47. c = (a + LCG.UONE) * c
  48. a = a * a
  49.  
  50. nskip = nskip >> LCG.UONE
  51.  
  52. self._seed = a_next * self._seed + c_next
  53.  
  54.  
  55. #%%
  56. np.seterr(over='ignore')
  57.  
  58. a = np.uint32(1664525)
  59. c = np.uint32(1013904223)
  60. seed = np.uint32(1)
  61.  
  62. rng = LCG(seed, a, c)
  63. q = [rng.next() for _ in range(0, 2500000)]
  64.  
  65. %%capture cap --no-stderr
  66. print(q)
  67.  
  68. with open('output5.txt', 'w') as f:
  69. f.write(cap.stdout)
  70.  
  71. dieharder -f output5.txt -a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement