#45184 [NoF->Ana]: algorithm to limit random numbers to a certain range is flawed

From: Date: Tue, 21 Oct 2008 00:35:32 +0000
Subject: #45184 [NoF->Ana]: algorithm to limit random numbers to a certain range is flawed
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-129904@lists.php.net to get a copy of this message
 ID:               45184
 Updated by:       pajoye@php.net
 Reported By:      kala at sankya dot com
-Status:           No Feedback
+Status:           Analyzed
 Bug Type:         Math related
 Operating System: Linux
 PHP Version:      5.2.6
-Assigned To:      
+Assigned To:      pajoye


Previous Comments:
------------------------------------------------------------------------

[2008-06-26 01:00:00] php-bugs at lists dot php dot net

No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".

------------------------------------------------------------------------

[2008-06-18 16:21:40] pajoye@php.net

Would you be interested in improving our code?

------------------------------------------------------------------------

[2008-06-05 11:49:10] kala at sankya dot com

Sorry the line 

    int val = (i/divisor)*10;

in the example program should be :

    int val = (i/divisor)*REQUESTED_RANGE;

for the program to work correctly for any other range other than 10.

------------------------------------------------------------------------

[2008-06-05 11:30:09] kala at sankya dot com

@pajoye : Yes I'm aware of the limitations of rand() (and LCG
generators in general), and have used MT extensively. The problem here
isn't with the RNG but with the algorithm used in converting the raw 31
bit random number to one within a given range. This algorithm is common
to both the rand() and mt_rand() functions that take min/max arguments
(both uses the RANGE_RANDOM macro defined in the file
"ext/standard/php_rand.h") . The only work around is to use the no
argument variant to get a 31 bit number and and do the scaling yourself.
(But obviously the better solution would be to fix the code itself :) )

------------------------------------------------------------------------

[2008-06-05 10:37:10] pajoye@php.net

Have you tried to use mt_rand instead?

Rand is known for having "issues".

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/45184

-- 
Edit this bug report at http://bugs.php.net/?id=45184&edit=1



Thread (13 messages)

« previous php.bugs (#129904) next »