Algorithm to generate pseudorandom numbers serie, reproducible knowing initial seed
Question
I have to write a lightweight algorithm to generate pseudorandom infinite numbers series, and strongly dependant by initial seed.
In python it should be like
seed = 3345 // Common number
generator = numgen(seed)
while True:
generator.getNext() // With the same seed it produce same numbers
As I wrote, with same seed it has to produce the same number series, even in different machines and in different times. Is there a standard patten for this or do I have to implement my own algorithm?
Solution
With the given requirements, you can go for the constant series
generator = itertools.repeat(seed)
This is
- lightweight
- not necessarily random
- strongly dependant on the initial seed
- produces the same series even on a different machine
Edit: To make this an actual answer, here is how to use Python's Mersenne Twister to generate a series of random numbers:
seed = 3345
maximum = 1000000
generator = random.Random(seed)
while True:
print generator.random(maximum)
Creating your own random.Random
instance ensures that no calls to the random number generator from elsewhere interfere with your random state.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow