-
-
Save vxgmichel/2f7cb31f32c5d92810d44ccca36c99b7 to your computer and use it in GitHub Desktop.
RNG benchmark
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
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