August 16th, 2000, 10:50 AM
-
For some reason random numbers don't seem to be that random in PHP (at least if the range like 2 numbers). It seems to favor the 1 instead of 2 about 90% of the time.
Anybody know why this is, or how to work around it? (I'd like to have about a 50/50 split.
August 16th, 2000, 11:11 AM
-
Nevermind. I forgot to seed the random number.
August 16th, 2000, 11:14 AM
-
Hi JonLed,
I don't agree with you.
I've done the following test.
- Generate 1 million numbers between 1 and 2
- Add it to the corresponding array element
- Evaluate the probability
the results:
1: 49.882%
2: 50.118%
Here is the code:
<?
define(MAXLOOP,1000000);
srand((double)microtime()*1000000);
$arr[1]=0;$arr[2]=0;
while($i++<MAXLOOP)
$arr[rand(1,2)]++;
echo "1: ".($arr[1]/MAXLOOP*100)."%n";
echo "2: ".($arr[2]/MAXLOOP*100)."%n";
?>
JBL
August 16th, 2000, 11:21 AM
-
Yes, I know. Read my message above.
srand((double)microtime()*1000000);
Is what I left out. (This seeds the random number).