note 39858 deleted from function.rand by irchtml

From: Date: Fri, 13 Feb 2004 05:30:08 +0000
Subject: note 39858 deleted from function.rand by irchtml
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-65081@lists.php.net to get a copy of this message
Note Submitter: greensleeves13@hotmail.com ---- 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)) ; }

« previous php.notes (#65081) next »