note 108304 added to function.mt-rand
| From: | scavenger | Date: | Sun, 15 Apr 2012 21:15:49 +0000 |
| Subject: | note 108304 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187154@lists.php.net to get a copy of this message | ||
to complete the very good idea from thegreatall at gmail dot com :
Because i didn't understand at all the idea behind the use of hex to generate randoms, and
worst the use of these specific numbers (0x1D39D3E06400000 and 0x41C21CB8E0FFFFFF), here is an
explanation on why this method still is the BEST and QUICKEST way to get alphanumerics randoms.
The following example function generates a pure random, exact 6 characters alphanumeric, on 32bit or
more OS:
<?
function random36($length = 6)
{
// 6 characters
$result = substr(base_convert(mt_rand(60466176, 2147483647), 10, 36), 0, $length);
// for 12 characters or more, simply duplicate the stuff
$result = substr(base_convert(mt_rand(60466176, 2147483647), 10, 36).base_convert(mt_rand(60466176,
2147483647), 10, 36), 0, $length);
}
?>
Randoms are between 100000 and zik0zj = 2087017471 possibilities
The reason we choose a random between 60466176 and 2147483647 exactly is based on the platform
limitation :
1) mt_getrandmax()
32bits : mt_getrandmax(); //2147483648
64bits : mt_getrandmax(); //18446744073709551616
2) mt_rand(in, between)
mt_rand(in, 2147483648) returns an error ! The maximum can only be 2147483647 : random from 0 to
2147483647 = 2147483648 possibilities :-)
3) maximum
base_convert(integer, 10, 36)
This convert a base 10 integer (0-9) to an alphanumeric base 36 number (0-z). Since we want an
always fixed length alphanumeric number, and depending on the OS limitations (here, 32bits), at
first sight the possible base 36 randoms we can have are between : 100000 and zzzzzz
But, since our maximum is 2147483647, ou base 36 maximum will be:
base_convert(2147483647, 10, 36); //zik0zj
4) minimum
Our base 10 minimum will be:
base_convert('100000', 36, 10); //60466176
5) QUICKEST way
Based on some 100000 times loop tests i made, with different functions on this page, the results are
clear :
Robin Leffmann : 1s (but it's binary)
thegreatall at gmail dot com : 1.8s
sheyd dot fr : 4.3s
james at coretelecom dot co dot uk : 27s
www.mrnaz.com : >30s
MagicalTux at ooKoo dot org : 7.3s
phil _ ozil at hotmail dot com : 4.9s
----
Server IP: 195.221.21.36
Probable Submitter: 82.244.50.105
----
Manual Page -- http://www.php.net/manual/en/function.mt-rand.php
Edit -- https://master.php.net/note/edit/108304
Del: integrated -- https://master.php.net/note/delete/108304/integrated
Del: useless -- https://master.php.net/note/delete/108304/useless
Del: bad code -- https://master.php.net/note/delete/108304/bad+code
Del: spam -- https://master.php.net/note/delete/108304/spam
Del: non-english -- https://master.php.net/note/delete/108304/non-english
Del: in docs -- https://master.php.net/note/delete/108304/in+docs
Del: other reasons-- https://master.php.net/note/delete/108304
Reject -- https://master.php.net/note/reject/108304
Search -- https://master.php.net/manage/user-notes.php