Working of rand() and mt_rand() functions in php

169 views Asked by At

The random number generation functions work based on the pseudorandom number generator (PRNG) algorithm. And according to this algorithm each value in the set from random number is to be chosen will have equal frequency in the output.

I would like to know why the same doesn't stand true when tested for small counts. Say I run a loop to print rand(1,2) ten times, I find an unequal frequency of occurrence for 1 and 2.

Does the point of equal frequency stand only when tested over a very large count value?

1

There are 1 answers

0
Mike Robinson On

I believe that you will find that what they say is that each number has equal probability, not equal frequency. This is, in other words, an "un-weighted probability distribution."

Consider "ten coin-flips." If the first five flips were Heads, then the remaining five would have to be Tails. (And the PRNG would have to somehow know that you intended to flip "ten" times.) This outcome would no longer be random at all, but pre-ordained.

As every gambler knows, the fact that a fair dice has 1/6 probability of landing sixes-up (or any-other number up ...) does not dictate what face will come up.