Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    92
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Is Rand() really bad or am I doing something wrong?

    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?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I just tried this and there doesn't seem to be any correlation. Are you sure that is your exact code?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    92
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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";

    }
    ?>

  • #4
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    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

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    92
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #6
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Try using mt_rand() instead and see if you get the same result.

    http://us3.php.net/mt_rand


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •