random number generation

Discussion in 'C++' started by Profil1, Mar 30, 2005.

  1. Profil1

    Profil1 Guest

    hi, is this the correct way to generate floating point random numbers >= 0.0
    and <= 1.0 that are different each time the program is run?

    #include <stdlib.h>
    #include <time.h>

    srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
    float random_value = rand()/RAND_MAX;
     
    Profil1, Mar 30, 2005
    #1
    1. Advertising

  2. Profil1 wrote:
    > hi, is this the correct way to generate floating point random numbers >= 0.0
    > and <= 1.0 that are different each time the program is run?
    >
    > #include <stdlib.h>
    > #include <time.h>
    >
    > srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
    > float random_value = rand()/RAND_MAX;


    I think generation of [pseudo-] random numbers has been covered
    extensively in many newsgroups to warrant another discussion on it.
    Please use the Web to search the answer to your FAQ.
     
    Victor Bazarov, Mar 30, 2005
    #2
    1. Advertising

  3. Profil1 wrote:
    >
    > hi, is this the correct way to generate floating point random numbers >= 0.0
    > and <= 1.0 that are different each time the program is run?
    >
    > #include <stdlib.h>
    > #include <time.h>
    >
    > srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
    > float random_value = rand()/RAND_MAX;


    No

    rand() returns an integer
    RAND_MAX is an integer

    So dividing an integer by another integer gives what?
    Knowing that RAND_MAX is the highest value that rand() might
    produce, what does this tell us about the outcome of the division.
    (eg. if the highest number equals 8, what is the result of
    2 / 8
    3 / 8
    7 / 8
    )

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 30, 2005
    #3
  4. Profil1 wrote:
    > hi, is this the correct way to generate floating point random numbers >= 0.0
    > and <= 1.0 that are different each time the program is run?
    >
    > #include <stdlib.h>
    > #include <time.h>
    >
    > srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??

    Yes, it should normally only be necessary to call srand once. However,
    the return value of time() will only change once per second, so if you
    start two instances of the program within one second, they will have the
    same sequence of random numbers.

    > float random_value = rand()/RAND_MAX;

    To be sure that you actually get a float, and not just something rounded
    to 0 or 1, you could write

    float random_value = static_cast<float>(rand())/RAND_MAX;

    instead.

    / martin
     
    Martin Magnusson, Apr 4, 2005
    #4
  5. Profil1

    msalters Guest

    Martin Magnusson wrote:

    > float random_value = static_cast<float>(rand())/RAND_MAX;


    Also consider
    const float inv_RAND_MAX = 1.0 / RAND_MAX;
    float random_value = rand()*inv_RAND_MAX;

    Might be more efficient, multiplication is usually cheaper and
    (almost?) never more expensive. I think it's not a legal
    optimalization for an IEEE-conforming compiler as the results
    might differ a bit, but in this case you don't care about it.

    Regards,
    Michiel Salters
     
    msalters, Apr 4, 2005
    #5
  6. Martin Magnusson wrote:
    > Yes, it should normally only be necessary to call srand once. However,
    > the return value of time() will only change once per second, so if you
    > start two instances of the program within one second, they will have the
    > same sequence of random numbers.


    If you want to use a higher resolution clock for the random seed, you
    can do like this

    #include <stdlib.h>
    #include <sys/time.h>
    #include <iostream>
    using namespace std;

    int main()
    {
    struct timeval time;
    gettimeofday( &time, 0 );
    srand( time.tv_usec );
    float random_value = static_cast<float>(rand())/RAND_MAX;
    cout << random_value << "\n";
    }


    / martin
     
    Martin Magnusson, Apr 4, 2005
    #6
  7. Profil1

    E. Mark Ping Guest

    In article <1112600410.38806c8dd45da8a3a4a37b6ab0a2ed61@teranews>,
    Martin Magnusson <> wrote:
    >Profil1 wrote:
    >> float random_value = rand()/RAND_MAX;

    >To be sure that you actually get a float, and not just something rounded
    >to 0 or 1, you could write
    >
    >float random_value = static_cast<float>(rand())/RAND_MAX;


    While this is common advice, and often works well in practice, this
    can cause serious numerical biases. For instance, if RAND_MAX=65536
    and you're selecting from 100,000 elements at random, you'll never
    select some of the elements.

    boost has done quite a bit to make these issues. See:
    <http://www.boost.org/libs/random/>
    --
    Mark Ping
     
    E. Mark Ping, Apr 4, 2005
    #7
    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. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,220
    vipinlal
    Mar 26, 2010
  2. ANM
    Replies:
    2
    Views:
    1,451
    Thomas Schodt
    Mar 7, 2004
  3. mescaline
    Replies:
    4
    Views:
    1,391
    Cy Edmunds
    Sep 10, 2003
  4. globalrev
    Replies:
    4
    Views:
    813
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,323
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page