How do I generate thread-safe uniform random numbers?

前端 未结 5 675
渐次进展
渐次进展 2020-12-01 03:53

My program needs to generate many random integers in some range (int min, int max). Each call will have a different range. What is a good (preferably thread-safe) w

5条回答
  •  孤城傲影
    2020-12-01 04:03

    Write a simple LCG (or whatever) PRNG for yourself, which will produce numbers up to the maximum possible required. Use a single static copy of the built-in RNG to seed a new local copy of your own PRNG for each new thread you generate. Each thread-local PRNG will have its own local storage, and never needs to refer to the central RNG again.

    This assumes that a statistically good RNG is fine for you and that cryptographic security is not an issue.

提交回复
热议问题