PDA

View Full Version : Determining the randomness of the rand function


AFHOF
07-23-2006, 09:31 AM
I was playing around in PHP today, and I decided to test how random the rand() function really was. I generated a 2 MB file made entirely of of random characters from the rand function. I then took the file and zipped it to about 50 KB. I beleive that ZIP filea are based of repeated sequences of characters, os would compression of this nature signify that the rand function isnt really all that random?

Exedore
07-23-2006, 12:19 PM
Nope. Zip files look at bit or byte sequences and basically associate the more common sequences with a shorter index. What you basically found is that files that consist of only ~10 characters represented in 8 bits compress very nicely because they can be stored in 4 bits.

Modred
07-23-2006, 01:24 PM
Any computer generated random numbers are determined based on an algorithm, making them psuedo-random. You can't avoid it. However, you can change what the algorithm produces by seeding the rand function with different numbers. It seems that PHP may seed rand automatically, but I know other languages like C++ do not.

Graeme
07-23-2006, 01:43 PM
. . .

Exedore
07-23-2006, 06:12 PM
You don't seed a pseudo-random number generator with another algorithm. A seed is a physically random phenomena like the time of day, temperature, etc that determines the starting point of the P-RNG.