note 73587 added to function.rand
| From: | PatrickDaryllG dot at osu1 dot php dot net | Date: | Fri, 02 Mar 2007 14:51:56 +0000 |
| Subject: | note 73587 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124013@lists.php.net to get a copy of this message | ||
Using rand()%x is faster than rand(0,x) yes, but it is wrong.
Consider the following example:
RAND_MAX is 32768 (like on Windows for example)
You use rand()%30000
Imagine rand() returns a value between 30000 and 32768.
Modulo could make any value between 0 and 2768, but not any between 2769 and 29999 (except the value
is below 29999).
This would double the chance of getting a number between 0 and 2768, which is speaking against the
principles of randomness.
----
Server IP: 217.160.72.57
Probable Submitter: 84.135.48.206
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/73587
Del: integrated -- https://master.php.net/note/delete/73587/integrated
Del: useless -- https://master.php.net/note/delete/73587/useless
Del: bad code -- https://master.php.net/note/delete/73587/bad+code
Del: spam -- https://master.php.net/note/delete/73587/spam
Del: non-english -- https://master.php.net/note/delete/73587/non-english
Del: in docs -- https://master.php.net/note/delete/73587/in+docs
Del: other reasons-- https://master.php.net/note/delete/73587
Reject -- https://master.php.net/note/reject/73587
Search -- https://master.php.net/manage/user-notes.php