Skip to content

Instantly share code, notes, and snippets.

@georgevreilly
Last active November 17, 2021 19:54
Show Gist options
  • Save georgevreilly/040195ef425ece3a10639b5554d5ffed to your computer and use it in GitHub Desktop.
Save georgevreilly/040195ef425ece3a10639b5554d5ffed to your computer and use it in GitHub Desktop.
#!/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