2 ms·
> Putting it another way, if you always start them in the same state, they'll always produce the same sequence. This can be easy to bump into in fairly trivial
by magicalhippo 2y ago
> Putting it another way, if you always start them in the same state, they'll always produce the same sequence.
This can be easy to bump into in fairly trivial cases.
For example, initializing by time alone can be problematic if you spawn N threads at once which all do their own seeding. Then they can all reach the seeding process at the same time, relative to the clock used for the seeding.
I had that problem with one of my earlier ray tracers, and so had to add add the thread id into the mix to seed the PRNGs.
- markh1967 2y agoA demonstration of exactly this problem (only in older versions of .Net, newer versions use a different seeding method) https://dotnetfiddle.net/tLGMp6 https://dotnetfiddle.net/tLGMp6
- mturmon 2y agoAgreed. I do a lot of Monte Carlo simulations of physical systems (radars, scientific models) in which same or close seeds will cause trouble. You do want a deterministic PRNG (for repeatability of the simulation). But if you seed with time alone, and spawn 100 parallel jobs, you will see repeats or near-repeats which will bias the Monte Carlo averages resulting from your simulation ensemble. In ancient times, you could xor in the PID and the IP address of the host to ensure no seed overlap. Nowadays there are other ways to introduce entropy to the seed, e.g. with dedicated system calls or /dev/random.