Last active
November 17, 2021 19:54
-
-
Save georgevreilly/040195ef425ece3a10639b5554d5ffed to your computer and use it in GitHub Desktop.
Linear Congruential Generator, Hull Dobel. https://en.wikipedia.org/wiki/Linear_congruential_generator#c_%E2%89%A0_0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
def lcg(a, c, m): | |
def _lcg(a, c, m): | |
x = random.randint(0, m-1) | |
for _ in range(m): | |
yield x | |
x = (a * x + c) % m | |
return [x for x in _lcg(a, c, m)] | |
def test_lcg(m): | |
check = list(range(m)) | |
errors = 0 | |
for e in range(m//8): | |
for c in range(1, m, 2): | |
a = 8 * e + 5 | |
permute = lcg(a, c, m) | |
if sorted(permute) == check: | |
result = "\033[32m" + "yes" + "\033[0m" | |
else: | |
result = "\033[31m" + "no" + "\033[0m" | |
errors += 1 | |
print("a={}, c={}, m={}, result={}, permute={}".format(a, c, m, result, permute)) | |
return errors | |
errors = test_lcg(64) | |
print(errors, "errors") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment