SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys,

    Can anyone figure out why the code always causes the number 1 to be generated?

    <html>
    <head></head>
    <body>
    <?php
    $number = rand(1,100);
    if($guess>$number) {
    echo "Your guess is too high";
    echo "<br>I was thinking of $number, you wasn't ";
    }
    if($guess<$number){
    echo "Your guess is too low";
    echo "<br>I was thinking of $number, you wasn't ";
    }
    ?>
    You win
    </body>
    </html>

  2. #2
    SitePoint Enthusiast ericd's Avatar
    Join Date
    Nov 2000
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just copied and pasted that code, and it will generate a number between 1 and 100 for me,,

    try it

    http://www.oriea.com.au/rand.php?guess=50


    Im using PHP3


  3. #3
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try placing:

    srand ((double) microtime() * 1000000);

    before you call the rand() function, you should only use the above code once per script execution though.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  4. #4
    SitePoint Evangelist Caesar's Avatar
    Join Date
    May 2000
    Location
    On some harddisk
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any explanation or reason for that?

    Also, the number generated is not from 1 to 100.
    What should I do?
    <Edited by Caesar on 01-06-2001 at 05:06 AM>

  5. #5
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can explain that...

    The function srand() seeds the random timer...ie sets it up.

    Dont ask me why they didnt just make it happen auto-styles when you call rand() but i think it makes it more **random**.
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What mad-onion said is absolutely correct, I just want to add some details.

    It needs random generator since machine CANNOT generate random numbers, it uses formula for doing such a thing, and with formula, the output is systematic.

    So the random generator needs a seed (normally people use unix time, which always increasing and somehow unique) to generate randomish values (which it uses some statistic distribution like exponential to obtain)

    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

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