Is PHP mt_rand really random or perhaps biased?

I did two basic ABC tests on my website with something like

if(mt_rand(0,2) == 0){
//THROW IN RE HERE 
}elseif(mt_rand(0,2) == 1){
//THROW IN LR HERE
}else{
//THROW IN LB HERE
}

I expected the three conditions to be repeated equally often (33.3% of all page views). However, the impressions (measured by Google Adsense) show very different distributions. Interestingly, both tests (two diagrams below) show similar patterns: LB is most common, then RE, and then LR.

The sample size is many thousands, so the likelihood that this will happen by accident is really zero.

Am I really misunderstanding mr_rand ()? Does anyone know if it is tested correctly? How else can these strange patterns appear?

enter image description here

+4
2

mt_rand . 0, 1 2. 0, RE. , (.. 1 2), ( 0, 1 2). 1, , LR. ( 0 2), LB. , .

    $number = mt_rand(0,2);
    switch ($number){
     case 0:
       //do re
       break;
     case 1:
       //do lr
       break;
     case 2:
       //do lb
       break;
    }

if(mt_rand(0,2) == 0){
//THROW IN RE HERE 
}elseif(mt_rand(0,1) == 1){ //we've stripped RE out, no longer deciding from 3 options
//THROW IN LR HERE
}else{
//THROW IN LB HERE
}
+4

, Google Adsense. Google Analytics, ? , , , PHP.

, PHP.

$test = [0,0,0];
for($i = 0; $i < 100000; $i++) {
    $rand = mt_rand(0,2);
    $test[$rand]++;
}
var_dump($test);

...

array(3) {
  [0]=>
  int(33288)
  [1]=>
  int(33394)
  [2]=>
  int(33318)
}

33%, 100 000 .

, mt_rand() - PRNG ( ), CSPRNG ( ). , , PRNG. Mersenne Twister, , libc rand(). , , , , PHP mt_rand().

+3

Source: https://habr.com/ru/post/1652908/


All Articles