How is a random number generated at runtime?

2019-01-11 23:58发布

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

Random.Next()

What happens inside?

7条回答
beautiful°
2楼-- · 2019-01-12 00:39

The Random class is a pseudo-random number generator.

It is basically an extremely long but deterministic repeating sequence. The "randomness" comes from starting at different positions. Specifying where to start is done by choosing a seed for the random number generator and can for example be done by using the system time or by getting a random seed from another random source. The default Random constructor uses the system time as a seed.

The actual algorithm used to generate the sequence of numbers is documented in MSDN:

The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

查看更多
登录 后发表回答