note 114999 added to function.mt-rand
| From: | bimal at sanjaal dot com | Date: | Sat, 10 May 2014 20:48:08 +0000 |
| Subject: | note 114999 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199507@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 92.97.207.228
----
Manual Page -- http://php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/114999
Del: integrated -- https://master.php.net/note/delete/114999/integrated
Del: useless -- https://master.php.net/note/delete/114999/useless
Del: bad code -- https://master.php.net/note/delete/114999/bad+code
Del: spam -- https://master.php.net/note/delete/114999/spam
Del: non-english -- https://master.php.net/note/delete/114999/non-english
Del: in docs -- https://master.php.net/note/delete/114999/in+docs
Del: other reasons-- https://master.php.net/note/delete/114999
Reject -- https://master.php.net/note/reject/114999
Search -- https://master.php.net/manage/user-notes.php