Skip to content

Instantly share code, notes, and snippets.

@vxgmichel

vxgmichel/rng.py Secret

Created March 24, 2018 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vxgmichel/2f7cb31f32c5d92810d44ccca36c99b7 to your computer and use it in GitHub Desktop.
Save vxgmichel/2f7cb31f32c5d92810d44ccca36c99b7 to your computer and use it in GitHub Desktop.
RNG benchmark
import os
from random import randint
from collections import Counter
def gen1(s):
while True:
s = s**7 % ~-2**67
yield s % 6 + 1
def gen2(s):
while True:
s = 256
while s >= 252:
s = os.urandom(1)[0]
yield s % 6 + 1
def gen3(s):
while True:
yield randint(1, 6)
def test(gen, n, i):
g = gen(id(i))
c = Counter(next(g) for _ in range(6*n))
return sum((x-n)**2 for x in c.values()) ** 0.5
def run(t=5, n=10**5):
for name, gen in (('short', gen1), ('urandom', gen2), ('randint', gen3)):
print(name, sum(test(gen, n, i)/t for i in range(t)))
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment