note 73429 added to function.rand
| From: | Niels at osu1 dot php dot net | Date: | Thu, 22 Feb 2007 22:54:32 +0000 |
| Subject: | note 73429 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123725@lists.php.net to get a copy of this message | ||
The example from "relsqui at armory dot com" is wrong. It talks about bit operations and
uses "&", but is actually an example of using modulus and should use "%".
The idea is that "rand()%(x+1)" is faster than "rand(0,x)". I agree, it is. But
not a lot.
Here's the fastest function I can think of for making pseudo-unique, valid IDs for HTML tags,
in which the first character can't be a number:
function makeID() {
$s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
return $s{rand()%52}.$s{rand()%62}.(repeat ad libitum).$s{rand()%62};
}
----
Server IP: 82.103.133.188
Probable Submitter: 62.79.44.136
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/73429
Del: integrated -- https://master.php.net/note/delete/73429/integrated
Del: useless -- https://master.php.net/note/delete/73429/useless
Del: bad code -- https://master.php.net/note/delete/73429/bad+code
Del: spam -- https://master.php.net/note/delete/73429/spam
Del: non-english -- https://master.php.net/note/delete/73429/non-english
Del: in docs -- https://master.php.net/note/delete/73429/in+docs
Del: other reasons-- https://master.php.net/note/delete/73429
Reject -- https://master.php.net/note/reject/73429
Search -- https://master.php.net/manage/user-notes.php