Alex Rivera | Logout

how does random() actually work?

Asked 2009-08-31T20:41:48.270
14

Every language has a random() function or something similar to generate a pseudo-random number. I am wondering what happens underneath to generate these numbers? I am not programming anything that makes this knowledge necessary, just trying to satisfy my own curiosity.

Edit
Report

2 Answers

4

The Wikipedia page is a good reference.

The actual algorithm used is going to be dependent on the language and the implementation of the language.

answered 2009-08-31T20:46:35.047
3

random() is a so called pseudorandom number generator (PRNG). random() is mostly implemented as a Linear congruential generator. This is a function of the form X(n+1) (aXn +c) modulo m. Xn is the sequence of generated pseudorandom numbers. The genarated sequence of numbers is easy guessable. This algorithm can't be used as a cryptographically safe PRNG.

Wikipedia:Linear congruential generator

And take a look at the diehard tests for PRNG PRNG Diehard Tests

answered 2009-08-31T20:54:10.227

Your Answer