Edit report at https://bugs.php.net/bug.php?id=45184&edit=1
ID: 45184 Updated by: php-bugs@lists.php.net Reported by: kala at sankya dot com Summary: algorithm to limit random numbers to a certain range is flawed -Status: Feedback +Status: No Feedback Type: Bug Package: Math related Operating System: Linux PHP Version: 5.2.6 Assigned To: pajoye New Comment: No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Open". Thank you. Previous Comments: ------------------------------------------------------------------------ [2010-05-21 12:14:13] m...@php.net Should probably be in feedback state. ------------------------------------------------------------------------ [2008-06-18 16:21:40] paj...@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] paj...@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 https://bugs.php.net/bug.php?id=45184 -- Edit this bug report at https://bugs.php.net/bug.php?id=45184&edit=1