note 39858 added to function.rand
| From: | greensleeves13 at hotmail dot com | Date: | Fri, 13 Feb 2004 03:48:28 +0000 |
| Subject: | note 39858 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-65077@lists.php.net to get a copy of this message | ||
if rand() can only give us a range of (0, 32767), then even if we invert the random number and
multiply it with a bigger number, we will still not do the bigger number honour since only 32768 of
those numbers will ever turn up.
don't know how more accurate this is, but i tried doing this for big(ger) random numbers -
function frand ()
{
# returns a random 30-bit float
# workaround rand() which seems to manage only
# (0, 32767) on some systems
#
// put together 2 15-bit numbers
// we need to do this cos otherwise the inverse
// of the random number we get wouldn't give us
// a random number space big enough to do multiplication
// against a 30-bit number.
$r1 = rand(0, 32767) << rand(0, 15);
$r2 = rand(0, 32767);
return (double) (1/($r1 + $r2)) ;
}
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39858
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39858&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39858&report=yes
Search -- http://master.php.net/manage/user-notes.php