srand - scope of its effects/are they global?

Discussion in 'C++' started by jdm, Apr 26, 2010.

  1. jdm

    jdm Guest

    Hi,

    I'm writing a program which will use rand() to generate some random
    numbers and srand plus time(0) to seed the PRNG. This raised a
    question: if I use srand in one function (eg main) which then calls
    another function in which the calls to rand() are made, will the
    reseeding I did in the calling function affect rand()'s behaviour?

    Or do I need to use srand() at the start of every function in which I
    use rand?

    (In other words, what is the scope of a PRNG reseeding?)
    --------------
    //Example:

    #include <cmath>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    void some_function(some parameters);

    int main(int argc, char *argv[])
    {
    time_t random_seed = time(0);
    if (seed == time_t(-1))
    {
    //do some error-handling
    }
    srand((unsigned int)random_seed);
    some_function(parameters);

    ...

    return 0;
    }

    void some_function(some parameters)
    {
    //do various things involving calls to rand()
    }
    -------------
    Thanks,

    James McLaughlin.

    PS. I will probably replace the calls to rand() and srand() with
    something from Boost later on, but I still thought this was worth
    finding out in case a similar issue occurred in the future.
     
    jdm, Apr 26, 2010
    #1
    1. Advertising

  2. jdm

    Öö Tiib Guest

    On Apr 27, 12:28 am, jdm <> wrote:
    > Hi,
    >
    > I'm writing a program which will use rand() to generate some random
    > numbers and srand plus time(0) to seed the PRNG. This raised a
    > question: if I use srand in one function (eg main) which then calls
    > another function in which the calls to rand() are made, will the
    > reseeding I did in the calling function affect rand()'s behaviour?
    >
    > Or do I need to use srand() at the start of every function in which I
    > use rand?


    srand() will seed pseudo-random-generator of standard library. That
    means everywhere in your program that uses rand() of same standard
    library. That usually means everywhere. However there exist eccentric
    cases when modules of same program manage to link themselves to
    different standard libraries.
     
    Öö Tiib, Apr 26, 2010
    #2
    1. Advertising

  3. jdm

    jdm Guest

    Thanks Paavo and Öö Tiib!
     
    jdm, Apr 27, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Asfand Yar Qazi
    Replies:
    4
    Views:
    432
    Asfand Yar Qazi
    Nov 12, 2004
  2. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,227
    Nick Keighley
    Oct 18, 2011
  3. thunk
    Replies:
    1
    Views:
    360
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    536
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    660
    thunk
    Apr 3, 2010
Loading...

Share This Page