summaryrefslogtreecommitdiff
path: root/lib/utils/prng.py
diff options
context:
space:
mode:
authorGeorge Steel <george.steel@gmail.com>2022-12-26 20:13:48 -0500
committerGeorge Steel <george.steel@gmail.com>2022-12-26 20:13:48 -0500
commite28ea888a9604052d6d7b94c8e29e34b74242994 (patch)
treed9c20a8b56aca08791cde310e47a0a562c5ad97b /lib/utils/prng.py
parentb63f19b2d0747769c60c8a2a52489ee30fa02a07 (diff)
use random oracle for randomized satin columns and redo split stitches
Diffstat (limited to 'lib/utils/prng.py')
-rw-r--r--lib/utils/prng.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/utils/prng.py b/lib/utils/prng.py
new file mode 100644
index 00000000..518049cb
--- /dev/null
+++ b/lib/utils/prng.py
@@ -0,0 +1,44 @@
+from hashlib import blake2s
+from math import ceil
+from itertools import count, chain
+import numpy as np
+
+
+def joinArgs(*args):
+ # Stringifies parameters into a slash-separated string for use in hash keys.
+ # Idempotent and associative.
+ return "/".join([str(x) for x in args])
+
+
+MAX_UNIFORM_INT = 2 ** 32 - 1
+
+
+def uniformInts(*args):
+ # Single pseudo-random drawing determined by the joined parameters.
+ # Returns 4 uniformly random uint64.
+ s = joinArgs(*args)
+ h = blake2s(s.encode()).hexdigest()
+ nums = []
+ for i in range(0, 64, 8):
+ nums.append(int(h[i:i+8], 16))
+ return np.array(nums)
+
+
+def uniformFloats(*args):
+ # returns an array of 8 floats in the range [0,1]
+ return uniformInts(*args) / MAX_UNIFORM_INT
+
+
+def nUniformFloats(n: int, *args):
+ # returns a fixed number (which may exceed 8) of floats in the range [0,1]
+ seed = joinArgs(*args)
+ nBlocks = ceil(n/8)
+ blocks = [uniformFloats(seed, x) for x in range(nBlocks)]
+ return np.concatenate(blocks)[0:n]
+
+
+def iterUniformFloats(*args):
+ # returns an infinite sequence of floats in the range [0,1]
+ seed = joinArgs(*args)
+ blocks = map(lambda x: list(uniformFloats(seed, x)), count(0))
+ return chain.from_iterable(blocks)