[PHP] Random number generation via /r/learnprogramming


[PHP] Random number generation

Hello

I am trying to build a PHP script that gives me some random numbers, but i have not yet found a function that actually looks random enough. i tested a few solutions i found by googling, but the result is the same every time – I get around the same amount of each number every time

The server is not running php7 yet, so i cant use the build in function "random_int($min, $max)"

To test i made a small script, all it does is generate 1 million random numbers between 0 and 40, and at the end print out stats on how many times each number came up

Here is the script:

#!/usr/bin/php <?php $counters = array(); for ($counter = 0; $counter <= 1000000; $counter++) { $random_number = mt_rand(0, 40); if (!empty($counters[$random_number])) { $counters[$random_number]++; } else { $counters[$random_number] = 1; } } ksort($counters, SORT_NUMERIC); foreach ($counters as $counter => $value) { echo $counter . " Hits: " . $value . "\n"; } ?> 

When i run this, i get ~24400 hits on each number, this does not look random to me:

0 Hits: 24195 1 Hits: 24379 2 Hits: 24306 3 Hits: 24713 4 Hits: 24597 5 Hits: 24601 6 Hits: 24352 7 Hits: 24204 8 Hits: 24331 9 Hits: 24553 10 Hits: 24273 11 Hits: 24471 12 Hits: 24227 13 Hits: 24283 14 Hits: 24461 15 Hits: 24006 16 Hits: 24343 17 Hits: 24514 18 Hits: 24424 19 Hits: 24381 20 Hits: 24441 21 Hits: 24431 22 Hits: 24676 23 Hits: 24399 24 Hits: 24349 25 Hits: 24139 26 Hits: 24348 27 Hits: 24383 28 Hits: 24496 29 Hits: 24452 30 Hits: 24812 31 Hits: 24426 32 Hits: 24353 33 Hits: 24350 34 Hits: 24233 35 Hits: 24457 15 Hits: 24547 16 Hits: 24405 17 Hits: 24353 18 Hits: 24529 19 Hits: 24345 20 Hits: 24432 21 Hits: 24606 22 Hits: 24293 23 Hits: 24584 24 Hits: 24298 25 Hits: 24363 26 Hits: 24489 27 Hits: 24174 28 Hits: 24164 29 Hits: 24190 30 Hits: 24448 31 Hits: 24660 32 Hits: 24364 33 Hits: 24420 34 Hits: 24255 35 Hits: 24215 36 Hits: 24372 37 Hits: 24432 38 Hits: 24211 39 Hits: 24304 40 Hits: 24197 

I expected to see that one number (or multiple) hit many more times than the others, or many less, and if i run the script again i expect the result to look different, but when i run this script multiple times, each number get hit ~24400 times, every time.

I know that i cant make true random in PHP because it will require an external source for random data, like lightning strikes. But can PHP at least make numbers thats more random than this, because this does not seem random?

Submitted July 13, 2017 at 09:12AM by _dev_random_
via reddit http://ift.tt/2t6V2ZG

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s