Random number generator and seeding

Discussion in 'C++' started by Joe, Nov 19, 2004.

  1. Joe

    Joe Guest

    Hi, I have been working on some code that requires a high use of random
    numbers within. Mostly I either have to either:
    1) flip a coin i.e. 0 or 1, or
    2) generate a double between 0 and 1.

    I have utilised the following random number source code
    http://www.agner.org/random/

    What I have found is that there is a problem with seeding. The code
    generates a seed based on time(0). I have found that I need to increment
    seed and

    A snippet of my code is below. I basically generate a Population object that
    contains various individuals. I want to randomly choose two individuals from
    this population. I wish I could choose these individuals by the following:

    // get two parents
    ind1 = pool.RouletteWheel();
    ind2 = pool.RouletteWheel();
     
    Joe, Nov 19, 2004
    #1
    1. Advertising

  2. Joe

    Chris Theis Guest

    "Joe" <> schrieb im Newsbeitrag
    news:...
    > Hi, I have been working on some code that requires a high use of random
    > numbers within. Mostly I either have to either:
    > 1) flip a coin i.e. 0 or 1, or
    > 2) generate a double between 0 and 1.
    >
    > I have utilised the following random number source code
    > http://www.agner.org/random/
    >
    > What I have found is that there is a problem with seeding. The code
    > generates a seed based on time(0). I have found that I need to increment
    > seed and
    >
    > A snippet of my code is below. I basically generate a Population object

    that
    > contains various individuals. I want to randomly choose two individuals

    from
    > this population. I wish I could choose these individuals by the following:
    >

    [SNIP]

    I guess it's necessary to clarify some things about random number
    generators. In principle they work the following way that you have to pass a
    starting seed which is the starting point of your sequence of (pseudo)random
    numbers. Passing the same seed to the RNG will result in the same sequence.
    This is useful if you need random numbers but simultaneously have the
    requirement of reproducing the behavior of your algorithm.

    However, normally the seed is passed only at the construction time and
    subsequent calls to the RNG's function which actually returns the numbers,
    should modify the internally stored seed of the RNG object. You should be
    aware that seeds are not numbers which will continuously increase or
    decrease. Furthermore, the seed has a great influence on the quality of the
    random sequence but this would lead too far here.

    In summary you should pass the seed only once to your random number object
    and also this object should be created only once.

    int Population :: RouletteWheel ( int seed )
    {
    TRandomMersenne rg(seed);
    return rg.Random();
    }

    This implementation creates a RNG object whenever RouletteWheel is called. I
    don't know about the details of your population class but you could put the
    TRandomMersenne object there as a member which is initialized with the seed
    in the constructor of "population".

    HTH
    Chris
     
    Chris Theis, Nov 19, 2004
    #2
    1. Advertising

  3. Joe

    Howard Guest

    "Joe" <> wrote in message news:...
    > Hi, I have been working on some code that requires a high use of random
    > numbers within. Mostly I either have to either:
    > 1) flip a coin i.e. 0 or 1, or
    > 2) generate a double between 0 and 1.
    >
    > I have utilised the following random number source code
    > http://www.agner.org/random/
    >
    > What I have found is that there is a problem with seeding. The code
    > generates a seed based on time(0). I have found that I need to increment
    > seed and
    >
    > A snippet of my code is below. I basically generate a Population object
    > that contains various individuals. I want to randomly choose two
    > individuals from this population. I wish I could choose these individuals
    > by the following:
    >
    > // get two parents
    > ind1 = pool.RouletteWheel();
    > ind2 = pool.RouletteWheel();
    > .
    > .
    > int Population :: RouletteWheel ( )
    > {
    > int seed = time(0);
    > TRandomMersenne rg(seed);
    > return rg.Random();
    > }
    >
    > In the above case all the random functions are contained in the Population
    > class. But when i do this it passes back two identical individuals. So
    > nstead I had to implement passing an incremented seed to the RouletteWheel
    > member function as shown below
    >

    <snip>
    >
    > I dont like this solution as it enforced defining random functions in the
    > main.cpp class - which i dont want. Is there any way to implement my
    > desired approach (the former). I dont like the idea of passing incremented
    > seeds .. even though it does appear to introduce randomness in my results.
    >
    > Any ideas greatly appreciated!
    >
    > Cheers,
    > Joe
    >


    You only want to seed a random-number generator once during a given run of a
    program. You then call the RNG multiple times to get "random" values.
    Seeding the RNG multiple times destroys the "randomness" of the RNG.

    -Howard
     
    Howard, Nov 19, 2004
    #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. Random Seeding

    , May 15, 2006, in forum: C Programming
    Replies:
    13
    Views:
    638
    Eric Sosman
    May 16, 2006
  2. Jack
    Replies:
    4
    Views:
    424
  3. HumanJHawkins
    Replies:
    2
    Views:
    519
    peter koch
    Nov 30, 2006
  4. Replies:
    3
    Views:
    588
    JimLewis
    May 7, 2009
  5. Carl Banks

    Re: Seeding the rand() Generator

    Carl Banks, Aug 3, 2009, in forum: Python
    Replies:
    4
    Views:
    481
    Nils Ruettershoff
    Aug 6, 2009
Loading...

Share This Page