5 ms·
PRNGs are one of those things that need to have global state if you want to get decent statistics out of them. You cannot have sets of threads fetching the sam
by pclmulqdq 2y ago
PRNGs are one of those things that need to have global state if you want to get decent statistics out of them. You cannot have sets of threads fetching the same value from the PRNG - it will totally destroy how random the PRNG is. Thread-local PRNGs with separate seeds is the way to go if you need parallelism, and it was the author's solution, but I can easily see not knowing that and running into this.
- randomonad 2y agoThanks, you answered the question I came here to ask, i.e. why did it need a lock for a random number, what are the tradeoffs of using the non locking version. Makes sense.
- stephc_int13 2y agoThe generator state should either be part of the API or be thread local, but there are probably complex implications regarding the long legacy of libc.
- loeg 2y agoYou can't seed each thread with the same value -- that's all you're saying, right?
- jayd16 2y agoResult distribution (like averaging to zero) only happens at scale. Even if you use a different seed every time you might not see the proper distribution when looking across different generator instances.
- pclmulqdq 2y agoCorrect, you must use different, uncorrelated seeds for each thread if you want this to work. You can seed them all from one CSPRNG, for example, or from the Linux entropy service.
- owlbite 2y agoAlgorithms for producing the same stream of random numbers in serial or parallel have been well known for many years. Perhaps the easiest in concept is "encrypt the sequence 1, 2, 3, 4, 5, ...".
- pclmulqdq 2y agoThe author of the blog wanted each thread to have different random numbers, not the same stream of numbers.
- Dylan16807 2y ago> You cannot have sets of threads fetching the same value from the PRNG - it will totally destroy how random the PRNG is. This is the rule you need to follow, but I don't understand what you're saying about global state. The broken version has global state. Non-broken versions can have global state or not have global state, depending on implementation choice. And that choice is separate from whether they have good performance.
- pclmulqdq 2y agoHow do you guarantee that you won't get the same value from a PRNG without global state? A PRNG is a state-updating machine that spits out a value based on that state.
- ayende 2y agoThread local state You don't need it global Seed it once per thread from /dev/random and you are done
- pclmulqdq 2y agoAs I suggested in the first comment here, yes. I believe the GP believed that you could have one PRNG that did not have global state for its consumers.
- Dylan16807 2y agoNo, I'm not saying that. I'm saying that if you have one PRNG, then you have global state no matter how it's designed. This is true whether you write it so that you get decent statistics, or you write it so you get tons of duplicate values. And many of the fixes remove global state. Per-thread PRNGs are one option, but so are PRNGs that are used by specific batches of objects. So, the straightforward broken option has global state, and the non-broken options might or might not have global state. Which means I have no idea what you're talking about when you use the phrase "need to have global state". What is the PRNG API where that need exists, and what does the version without global state look like?
- matheusmoreira 2y agoIs there a reason why the RNG function would not be able to simply take the random seed as one of its arguments?