Member-only story
Python Pseudorandom Bytes
A new algorithm for customizable, fast pseudorandom bytes that passes the tests with flying colors.
Randomness is too important to leave to chance. Donald Knuth said that, and it’s a good mantra for programmers to adhere to. Generating pseudorandom numbers is a notoriously error prone challenge, and getting it right is extremely important.
The following very short pseudorandom byte generator Python class is easy to implement, and easy to analyze for any shortcomings, because it’s so simple compared to more complex algorithms. Most importantly, it passes standard tests of randomness, such as the software suite of tests provided by John Walker at Fourmilab in Switzerland.
I’ve implemented the algorithm as a Python class called Prb (Pseudo-random bytes) making it easy to import into your own projects. I’ll present the code first, and then explain how it works, and why it works as well as it does.
# prb.pyclass Prb:
def __init__(self, seed=""):
unique_seed = str(seed)+ "Customize here"
len_unique_seed = len(unique_seed)
self.p, self.q = 0, 0
self.buf = list(range(256))
for i in range(997)…