Faisal
Faisal

Reputation: 4264

Algorithm to generate a random number? Don't use System.Random

Had there been no System.Random class, how would have you generated a random number?

Are there any known algorithms or have you guys ever designed one?

Upvotes: 3

Views: 1775

Answers (4)

Dimitar
Dimitar

Reputation: 2402

Like the other answers say, its not possible to generate true random numbers with a computer, but as for pseudo-random, one algorithm that I've used before is the Linear Congruential Generator, its simple and fast, but I'm sure there are much better alternatives.

Edit: Grammar

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 53699

Take a look at the Mersenne Twister. I belive this is the same algorithm implemented by System.Random, it is very common non cryptographically secure PRNG with a good random distribution.

Upvotes: 1

Matthew King
Matthew King

Reputation: 5194

You can't really generate a truly random number with our current deterministic computers. There are, however, many different ways to generate pseudorandom numbers. See pseudorandom number generator on wikipedia for some information on the algorithms.

Upvotes: 1

True random numbers can only be generated "outside" a computer, using radioactivity counts and such. Some VIA processors have hardware to do so.

Volume two of The Art of Computer Programming by Don Knuth spends a lot of time discussing exhaustively various pseudo-random number implementations from a mathematical background. Recommended reading.

Upvotes: 7

Related Questions