SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: srand() issue

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    srand() issue

    Does anybody have any idea why the result of following code is different each time:

    PHP Code:
    <?php

    srand
    (152);
    echo 
    rand();
    echo 
    '<br>';

    srand(152);
    echo 
    rand();
    echo 
    '<br>';

    srand(152);
    echo 
    rand();
    echo 
    '<br>';

    srand(152);
    echo 
    rand();

    ?>
    Code:
    1515441909
    654160073
    1204705132
    711690770
    
    213509977
    902459421
    1801474686
    900418819
    
    ....

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,901
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    The manual on srand suggests (http://php.net/manual/en/function.srand.php) that your PHP install might be running suhosin in which case srand doesn't do anything.

    If you want a random number generator that can be seeded have a look at this article: http://www.sitepoint.com/php-random-number-generator/
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Maybe i missed something here... but...
    "the result of following code is different each time"
    ... is this not the desired result of rand?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,901
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    Maybe i missed something here... but...
    "the result of following code is different each time"
    ... is this not the desired result of rand?
    Yes, but srand seeds rand, gives it a fixed starting point so to speak, which is wanted for certain experiments that need to be repeatable so peers can check your results.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    except that you cant predict the server load, which would slow your script and return differing values of rand....

    so... i'm still lost. If you want repeatability, used a fixed number set (which can be Rand-constructed)...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,901
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    except that you cant predict the server load, which would slow your script and return differing values of rand....
    You're assuming here that rand() uses the current time every time it gives you a random number. It doesn't. It starts off with a certain number, called the seed (which is indeed usually based on the current timestamp) and from there on it will just uses a formula with the previous number as input to generate the next number.

    Consider this:

    PHP Code:
    <?php

    srand
    (2);
    echo 
    rand(), "\n";
    echo 
    rand(), "\n";

    srand(2);
    echo 
    rand(), "\n";
    sleep(2);
    echo 
    rand(), "\n";
    This outputs the following, each and every time I run it (Kubuntu 32 bit 3.2.0-34-generic-pae):

    Code:
    1505335290
    1738766719
    1505335290
    1738766719
    So we can safely conclude that the time between rand()-calls doesn't matter.

    Quote Originally Posted by StarLion View Post
    so... i'm still lost. If you want repeatability, used a fixed number set (which can be Rand-constructed)...
    You could also do that, but saying "I used Mersenne_twister with a seed of 932382" is a lot easier than having to give someone else a list of numbers you used. Which may be thousands upon thousands of numbers, depending what you used them for.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guys, I'm in special case that I want to have fixed numbers by rand() function, in many servers I've got fixed numbers in following code:

    PHP Code:
    php -"srand(152); echo rand();" 
    Where output is always 979730751, like http://y-shahinzadeh.ir/srand.php

    I've tested in many places, however, in last server that I wanted to deploy my code, it resulted in not having fixed numbers:

    Code:
    root@portal:~# php -r "srand(152); echo rand();"
    46330306root@portal:~# php -r "srand(152); echo rand();"
    1169457841root@portal:~# php -r "srand(152); echo rand();"
    814871624root@portal:~# php -r "srand(152); echo rand();"
    1096079710root@portal:~# php -r "srand(152); echo rand();"
    323949937root@portal:~# php -r "srand(152); echo rand();"
    1595318222root@portal:~#

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,901
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by php_lover View Post
    guys, I'm in special case that I want to have fixed numbers by rand() function, in many servers I've got fixed numbers in following code:

    PHP Code:
    php -"srand(152); echo rand();" 
    Where output is always 979730751, like http://y-shahinzadeh.ir/srand.php

    I've tested in many places, however, in last server that I wanted to deploy my code, it resulted in not having fixed numbers:

    Code:
    root@portal:~# php -r "srand(152); echo rand();"
    46330306root@portal:~# php -r "srand(152); echo rand();"
    1169457841root@portal:~# php -r "srand(152); echo rand();"
    814871624root@portal:~# php -r "srand(152); echo rand();"
    1096079710root@portal:~# php -r "srand(152); echo rand();"
    323949937root@portal:~# php -r "srand(152); echo rand();"
    1595318222root@portal:~#
    I know. I answered in post #2

    Quote Originally Posted by ScallioXTX View Post
    The manual on srand suggests (http://php.net/manual/en/function.srand.php) that your PHP install might be running suhosin in which case srand doesn't do anything.

    If you want a random number generator that can be seeded have a look at this article: http://www.sitepoint.com/php-random-number-generator/
    Try to find out if the server does indeed have suhosin installed and if you access to php.ini try adding this to it:

    Code:
    suhosin.srand.ignore off
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank in advance, I accomplished addressing this issue, it was due to SuHosin as ScallioXTX said.


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
  •