views:

115

answers:

4

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?

+5  A: 

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.

Thorbjørn Ravn Andersen
+1 you can't get a real random number via any programming language.
Danny Chen
Via any _deterministic_ programming language. But many aren't, in fact. E.g. create two threads, and let them race to a mutex. You get less than one bit of entropy that way, but it is really random.
MSalters
A: 

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.

Joviee
+1  A: 

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.

Chris Taylor
+1  A: 

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

Dimitar