note 114999 deleted from function.mt-rand by cmb
| From: | cmb@php.net | Date: | Sun, 31 Mar 2019 08:34:06 +0000 |
| Subject: | note 114999 deleted from function.mt-rand by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210457@lists.php.net to get a copy of this message | ||
Note Submitter: bimal at sanjaal dot com
----
I was seeing how much unique and random 5-digits number I can use/generate in one-second's
interval of time. (I had another logic for myself to make my number unique based on the date/time
stamp value as well).
I found that the mt_rand() numbers are not quite unique. The repetitions will often occur a lot of
times even in the first 50 random numbers generated in a loop.
<?php
header('Content-Type: text/plain');
$errors = array();
$uniques = array();
for($i = 0; $i < 500; ++$i)
{
$random_code = mt_rand(10000, 99999);
if(!in_array($random_code, $uniques))
{
$uniques[] = $random_code;
}
else
{
$errors[] = $random_code;
}
}
/**
* If you get any data in this array, it is not exactly unique
* Run this script for few times and you may see some repeats
*/
print_r($errors);
?>