...

View Full Version : Is Rand() really bad or am I doing something wrong?



bfinke
06-17-2010, 01:27 AM
I have the following script:

$user = 1000;
$ender = 50*user;

for($i=0; $i < $ender; $i++){

$var1 = rand(1, ($user/5));
$var2 = rand(1, $user);

echo "<br> $i. $var1 and $var2";

}

If $user < 1000, then there appears to be no corellation between $var1 and $var2.

However, if $user > 10000 then any number for $var1 will always be associated with the same number for $var2 - in other words, for every round where $var 1 is "640", the value of $var2 will be either 1773 or 417 (EVERYTIME).

Am I doing something wrong?

_Aerospace_Eng_
06-17-2010, 01:38 AM
I just tried this and there doesn't seem to be any correlation. Are you sure that is your exact code?

bfinke
06-17-2010, 02:54 AM
yes, this is the exact code (I'll copy again to ensure its right):

<?php
$user = 10000;
$ender = 50*$user;

for($i=0; $i < $ender; $i++){

$var1 = rand(1, ($user/5));
$var2 = rand(1, $user);

echo "<br> $i. $var1 and $var2";

}
?>

Jazz914
06-17-2010, 03:05 AM
Seems fine for me, here's my first 30 results:

0. 774 and 783
1. 1912 and 4105
2. 1647 and 5118
3. 1534 and 1126
4. 814 and 9121
5. 1918 and 3283
6. 1961 and 2479
7. 250 and 7764
8. 1777 and 9880
9. 720 and 6757
10. 1947 and 1012
11. 514 and 7449
12. 1161 and 561
13. 1819 and 2028
14. 1105 and 8216
15. 1824 and 7681
16. 467 and 8664
17. 715 and 6596
18. 935 and 1593
19. 1681 and 5960
20. 1195 and 1689
21. 908 and 7960
22. 936 and 8641
23. 1584 and 9785
24. 845 and 7136
25. 1898 and 3622
26. 609 and 6861
27. 1035 and 6658
28. 917 and 2504
29. 1184 and 1081
30. 1455 and 3753

bfinke
06-17-2010, 04:37 AM
it's definitely giving me this issue, would there be a php.ini setting that would cause this?

I'm using WAMP, could that be responsible?

Zoic
06-17-2010, 06:57 AM
Try using mt_rand() instead and see if you get the same result.

http://us3.php.net/mt_rand



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum