Seeding Random Numbers in Multiple Threads?

Discussion in 'C++' started by HumanJHawkins, Nov 30, 2006.

  1. I wrote this question a little differently in the MFC group since it is
    a bit of a different environment than pure C++, but I hope you will
    forgive the similarities if anyone is reading both groups.

    Basically, I need to seed the random number generator in 4 seperate
    threads at once. So, I can't use the current time as the random seed,
    because then all four threads end up with the same series of (therefore
    non-random) numbers.

    Is there a best practice for seeding multiple random number generators
    at once?

    Thanks in advance!
     
    HumanJHawkins, Nov 30, 2006
    #1
    1. Advertising

  2. HumanJHawkins

    Steve Pope Guest

    HumanJHawkins <> wrote:

    >I wrote this question a little differently in the MFC group since it is
    >a bit of a different environment than pure C++, but I hope you will
    >forgive the similarities if anyone is reading both groups.
    >
    >Basically, I need to seed the random number generator in 4 seperate
    >threads at once. So, I can't use the current time as the random seed,
    >because then all four threads end up with the same series of (therefore
    >non-random) numbers.
    >
    >Is there a best practice for seeding multiple random number generators
    >at once?


    Obtain "seed" from the current time, then use

    seed
    seed + 1000
    seed + 2000
    seed + 3000

    Steve
     
    Steve Pope, Nov 30, 2006
    #2
    1. Advertising

  3. HumanJHawkins

    peter koch Guest

    HumanJHawkins skrev:
    > I wrote this question a little differently in the MFC group since it is
    > a bit of a different environment than pure C++, but I hope you will
    > forgive the similarities if anyone is reading both groups.
    >
    > Basically, I need to seed the random number generator in 4 seperate
    > threads at once. So, I can't use the current time as the random seed,
    > because then all four threads end up with the same series of (therefore
    > non-random) numbers.

    If you are talking about the standard random generator and srand, you
    better check the implementation. Most probably it is not thread-safe.
    In any case, by protecting rand() with a mutex (of some type - a
    windows critical_section looks best), there is no reason to seed more
    than once.
    >
    > Is there a best practice for seeding multiple random number generators
    > at once?

    If you use e.g. boost and let each thread have its own generator you
    could e.g. use time and add the threadid to that value.

    /Peter
     
    peter koch, Nov 30, 2006
    #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. Joe
    Replies:
    2
    Views:
    492
    Howard
    Nov 19, 2004
  2. Random Seeding

    , May 15, 2006, in forum: C Programming
    Replies:
    13
    Views:
    631
    Eric Sosman
    May 16, 2006
  3. Jack
    Replies:
    4
    Views:
    421
  4. (-Peter-)

    seeding random numbers

    (-Peter-), Feb 20, 2008, in forum: Java
    Replies:
    18
    Views:
    608
    John W. Kennedy
    Feb 22, 2008
  5. Replies:
    3
    Views:
    577
    JimLewis
    May 7, 2009
Loading...

Share This Page