note 78986 added to function.rand
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 06 Nov 2007 09:24:47 +0000 |
| Subject: | note 78986 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132027@lists.php.net to get a copy of this message | ||
Note, the function used by PHP to constrain a random number between (min, max) is the following:
$number = $min + (($max - $min + 1) * ($number / ($rand_max + 1));
where the following:
$number - the initially generated random number
$min - the minimum in the range
$max - the maximum in the range
$rand_max - the maximum possible random value
What this algorithm does is constrain the generated number to a 0-1 range, then multiply it against
your range, mapping the two to each other.
In practice you'll see the following results:
For a generated $number of 16384 and a $rand_max of 32768:
rand(0, 10) = 5
rand(0, 100) = 50
rand(0, 1000) = 500
with the additional property that if the range you're asking for is larger than $rand_max,
random numbers will be in a multiple of $max/$rand_max.
----
Server IP: 87.124.64.96
Probable Submitter: 62.3.216.18
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/78986
Del: integrated -- https://master.php.net/note/delete/78986/integrated
Del: useless -- https://master.php.net/note/delete/78986/useless
Del: bad code -- https://master.php.net/note/delete/78986/bad+code
Del: spam -- https://master.php.net/note/delete/78986/spam
Del: non-english -- https://master.php.net/note/delete/78986/non-english
Del: in docs -- https://master.php.net/note/delete/78986/in+docs
Del: other reasons-- https://master.php.net/note/delete/78986
Reject -- https://master.php.net/note/reject/78986
Search -- https://master.php.net/manage/user-notes.php