dimanche 25 octobre 2015

Why is 0 so frequent is this number generation pattern?

I was just goofing around with PHP and I decided to generate some random numbers with PHP_INT_MIN (-9223372036854775808) and PHP_INT_MAX (9223372036854775807). I simply echoed the following:

echo rand(-9223372036854775808, 9223372036854775807);

I kept refreshing to see the numbers generated and to view the randomness of the numbers, as a result I started to notice a pattern emerging. Every 2-4 refreshes 0 appeared and this happened without fail, at one stage I even got 0 to appear 4x in a row.

I wanted to experiment further so I created the following snippet:

<?php
$countedZero = 0;
$totalGen = 250;

for ($i = 1; $i <= $totalGen; $i++) {
    $rand = rand(-9223372036854775808, 9223372036854775807);

    if ($rand == 0) {
        echo $i . ": <font color='red'>" . $rand . "</font><br/>";

        $countedZero++;
    } else {
        echo $i . ": " . $rand . "<br/>";
    }
}
echo "0 was generated " . $countedZero . "/" . $totalGen . " times which is " . (($countedZero / $totalGen) * 100) . "%."
?>

this would give me a clear idea of what the generation rate is. I ran 8 tests:

  • The first 3 tests were using a $totalGen of 250. (3 tests total).

  • The second 3 tests were using a $totalGen of 1000. (6 tests total).

  • The third test was just to see what the results would be on a larger number, I chose 10,000. (7 tests total).

  • The fourth test was the final test, I was intrigued at this point because the last (large number) test got such a high result surprisingly so I raised the stakes and set $totalGen to 500,000. (8th test total).

Results

I took a screenshot of the results. I took the first output, I didn't keep testing it to try and get it to fit a certain pattern:

Test 1 (250)

(1).

(2).

(3).

Test 2 (1000)

(1).

(2).

(3).

Test 3 (10,000)

(1).

Test 4 (500,000)

(1).

From the above results, it is safe to assume that 0 has a very high probability of showing up even when the range of possible numbers is at its maximum. So my question is:

Is there a logical reason to why this is happening?

Considering how many numbers it can choose from why is 0 a recurring number?

Note Test 8 was originally going to be 1,000,000 but it lagged out quite badly so I reduced it to 500,000 if someone could test 1,000,000 and show the results by editing the OP it would be much appreciated.




Aucun commentaire:

Enregistrer un commentaire