W
W
Web Lizard2017-05-19 19:38:54
PHP
Web Lizard, 2017-05-19 19:38:54

What determines the correlation in the loss of random numbers?

In short, I wrote a program in php. Here is her code.

<?
$one = 0;
$two = 0;
for ($i=0; $i < 10000; $i++) { 
  $a = rand(1,2);
  if ($a == 1) {
    $one++;
  } else {
    $two++;
  }
}
echo "1:".$one." | 2: ".$two;
?>

The program displays how many times a single fell out, and what a deuce.
Having launched this program several dozen times, I noticed that such values ​​\u200b\u200bare falling out.

1:5082 | 2:4918
1:5052 | 2:4948
1:5033 | 2:4967
1:4945 | 2:5055
1:5010 | 2:4990

That is, the correlation never exceeds 100 units. Not to mention that it never drops an equal amount. Those. 5000 and 5000.
But why?? Why not 400 units? Not 500 or 600? Why so even?
And why if there is a 50% chance of the number 1 falling out, then the number 1 cannot fall out 100% of the time?
Explain, please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vasiliev, 2017-05-20
@Lizard-108

What you call correlation is better called scatter.
The distribution of the random variable "number of occurrences of units" is described by the binomial distribution .
The mathematical expectation of such a value is n * p, in your case n=10000, p=0.5, it turns out that on average one will appear 5000 times in a series of 10000 trials.
The spread can be estimated by calculating the standard deviation. For the binomial distribution, it is equal to sqrt(n*p*(1-p)), in your case it turns out 50. The rule of three sigma in this case should apply, so with a probability of 68% the average value of the "number of occurrences of ones" will be different from 5000 no more than 50, and with a 99% probability no more than 150. This agrees with the numbers you got in your experiment.

D
Denis Zagaevsky, 2017-05-19
@zagayevskiy

All this is explained by the fact that rand returns not a random number, but a pseudo-random number. PHP implements one of the pseudo-random number generators ( PRNG ). And the desired behavior is inherent in real random numbers.
The simplest toy PRNG might look like this

var next = 0;
for(;;){ 
    next = (next*9 + 3) % 256; 
}

next will each time have a number that is not easy to predict without knowing the algorithm.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question