2017-10-11 04:46:07 +00:00
|
|
|
__author__ = "Tobias Carryer"
|
|
|
|
|
|
|
|
from time import time
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-01-03 14:25:36 +00:00
|
|
|
class LinearCongruentialGenerator:
|
2017-10-11 04:46:07 +00:00
|
|
|
"""
|
|
|
|
A pseudorandom number generator.
|
|
|
|
"""
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2021-08-31 05:56:19 +00:00
|
|
|
# The default value for **seed** is the result of a function call which is not
|
|
|
|
# normally recommended and causes flake8-bugbear to raise a B008 error. However,
|
|
|
|
# in this case, it is accptable because `LinearCongruentialGenerator.__init__()`
|
|
|
|
# will only be called once per instance and it ensures that each instance will
|
|
|
|
# generate a unique sequence of numbers.
|
|
|
|
|
|
|
|
def __init__(self, multiplier, increment, modulo, seed=int(time())): # noqa: B008
|
2017-10-11 04:46:07 +00:00
|
|
|
"""
|
|
|
|
These parameters are saved and used when nextNumber() is called.
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2017-10-11 04:46:07 +00:00
|
|
|
modulo is the largest number that can be generated (exclusive). The most
|
2020-01-18 12:24:33 +00:00
|
|
|
efficient values are powers of 2. 2^32 is a common value.
|
2017-10-11 04:46:07 +00:00
|
|
|
"""
|
|
|
|
self.multiplier = multiplier
|
|
|
|
self.increment = increment
|
|
|
|
self.modulo = modulo
|
|
|
|
self.seed = seed
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def next_number(self):
|
2017-10-11 04:46:07 +00:00
|
|
|
"""
|
|
|
|
The smallest number that can be generated is zero.
|
2020-06-16 08:09:19 +00:00
|
|
|
The largest number that can be generated is modulo-1. modulo is set in the
|
|
|
|
constructor.
|
2017-10-11 04:46:07 +00:00
|
|
|
"""
|
|
|
|
self.seed = (self.multiplier * self.seed + self.increment) % self.modulo
|
|
|
|
return self.seed
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-10-11 04:46:07 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# Show the LCG in action.
|
2019-10-05 05:14:13 +00:00
|
|
|
lcg = LinearCongruentialGenerator(1664525, 1013904223, 2 << 31)
|
|
|
|
while True:
|
|
|
|
print(lcg.next_number())
|