How is a random number generated at runtime?

后端 未结 7 1395
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 14:28

Since computers cannot pick random numbers(can they?) how is this random number actually generated. For example in C# we say,

Random.Next()

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 15:28

    Computers use pseudorandom number generators. Essentially, they work by start with a seed number and iterating it through an algorithm each time a new pseudorandom number is required.

    The process is of course entirely deterministic, so a given seed will generate exactly the same sequence of numbers every time it is used, but the numbers generated form a statistically uniform distribution (approximately), and this is fine, since in most scenarios all you need is stochastic randomness.

    The usual practice is to use the current system time as a seed, though if more security is required, "entropy" may be gathered from a physical source such as disk latency in order to generate a seed that is more difficult to predict. In this case, you'd also want to use a cryptographically strong random number generator such as this.

提交回复
热议问题