initial commit

This commit is contained in:
2025-12-10 13:38:11 +11:00
commit 53852868aa
17 changed files with 370571 additions and 0 deletions

21
longrandom.py Normal file
View File

@@ -0,0 +1,21 @@
import math, os
from functools import reduce
log256 = math.log(256)
class LongRandom:
def getint(self, bytes):
value = list(os.urandom(bytes))
return reduce(lambda x,y: (x<<8)|y, value)
def get(self, modulus):
# Find the largest power-of-2 that modulus fits into
bytes = int(math.ceil(math.log(modulus) / log256))
maxval = 256 ** bytes
# maxmod is the largest multiple of modulus not greater than maxval
maxmult = maxval - maxval % modulus
while True:
value = self.getint(bytes)
# Stop generating when the value would not cause bias
if value <= maxmult:
break
return value % modulus