note 108683 added to function.mt-rand
| From: | scavenger | Date: | Thu, 17 May 2012 11:59:01 +0000 |
| Subject: | note 108683 added to function.mt-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188029@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 GOAL : on 32bit OS, get a 6+ characters random made of base36 alphanumerics [0-z]
The following function generates a pure random, exact 6 characters alphanumeric, on 32bit, which is
64bit and more compatible :
<?php
// 6 characters max; length of the wanted random can still be passed through the function
function random36($length = 6) {
return substr(base_convert(mt_rand(60466176, 2147483647), 10, 36), 0, $length);
}
// now for the trick : for 12 characters max or more, simply duplicate the stuff :
// => Generated randoms are between 100000 and zik0zj = 2087017471 possibilities
function random36($length = 12) {
return substr(base_convert(mt_rand(60466176, 2147483647), 10, 36).base_convert(mt_rand(60466176,
2147483647), 10, 36), 0, $length);
}
?>
The reason we choose a random between 60466176 and 2147483647 exactly is based on the platform
limitation :
1) mt_getrandmax()
<?php
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 :-) yes it's vicious
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: 69.147.83.197
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/108683
Del: integrated -- https://master.php.net/note/delete/108683/integrated
Del: useless -- https://master.php.net/note/delete/108683/useless
Del: bad code -- https://master.php.net/note/delete/108683/bad+code
Del: spam -- https://master.php.net/note/delete/108683/spam
Del: non-english -- https://master.php.net/note/delete/108683/non-english
Del: in docs -- https://master.php.net/note/delete/108683/in+docs
Del: other reasons-- https://master.php.net/note/delete/108683
Reject -- https://master.php.net/note/reject/108683
Search -- https://master.php.net/manage/user-notes.php