-2

I would like to generate several uniformly distributed random numbers in the same pass. So far I have a "standard" function for generating a random number

double generateRandomNumber()
{
    srand((unsigned)time(NULL));
    double r=((double)rand()/(double)RAND_MAX);
    return r;
}

how ever when in main I call it like that:

# include <iostream>
# include <string>
# include <cmath>
# include <ctime>

int main()
{

    // generate random number
    double rr1=generateRandomNumber();
    double rr2=generateRandomNumber();
    cout << rr1 << endl;
    cout << rr2 << endl;
    return 0;
}

I get that both numbers are the same ( I guess its the time limitations of seconds), anyways, this is something I would like to generelize to multiple random numbers.

Can anyone suggest a better way? maybe using a different method or library?

jarhead
  • 1,821
  • 4
  • 26
  • 46
  • @AnastasiyaAsadullayeva, Cheers, I'll check it out – jarhead Sep 25 '15 at 06:04
  • There are hundreds of Q&A on here (e.g. [this](http://stackoverflow.com/questions/6729214/rand-function-returns-same-values-when-called-within-a-single-function-c)) explaining how `srand()` should be called once (typically just inside `main()`), as it resets the random number sequence and calls with the same seed will restart at the same point therein. – Tony Delroy Sep 25 '15 at 06:04
  • 1
    http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once – David Heffernan Sep 25 '15 at 06:18

1 Answers1

1

Do not call srand every time beore using rand.
srand should be call only once on the program begins.

You can use static variable to see if srand is previously called.

double generateRandomNumber()
{
    static bool initialized = false;
    if (!initialized)
    {
        srand((unsigned)time(NULL));
        initialized = true;
    }
    double r=((double)rand()/(double)RAND_MAX);
    return r;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • @ MikeCAT why do you declare the static variables every time you enter the function, should'nt it be declared once? – jarhead Sep 25 '15 at 06:24
  • 1
    Hmmm, I can say that this is bad idea if there are multiple random numbber generating functions such as `int generateRandomInteger()`. Calling `srand` at the beginning of `main` function or wrapping `rand()` with a program which calls `srand` if needed is better. – MikeCAT Sep 25 '15 at 06:27
  • anyways , this does not solve the problem, this part runs fast, and the random numbers I get are still the same. – jarhead Sep 25 '15 at 06:27
  • This code [seems working well in Wandbox](http://melpon.org/wandbox/permlink/E5gALbxGJwExCTpM9). – MikeCAT Sep 25 '15 at 06:29
  • @jarhead Stop calling srand more than once! It is also clear that you don't understand how static local variables work. You really should put that right. – David Heffernan Sep 25 '15 at 06:37
  • Yeah, it works fine now, Cheers – jarhead Sep 25 '15 at 06:37
  • @MikeCAT, I just don't get yet the procedure that is done here....could you elaborate on that more? – jarhead Sep 25 '15 at 06:39
  • @DavidHeffernan, If you want to help then help, atm your comments are not useful at all – jarhead Sep 25 '15 at 06:41
  • @jarhead If you want to learn, then you'll heed my advice and learn about static locals. – David Heffernan Sep 25 '15 at 06:42
  • 1
    `static` variables in functions are initialized only once at the beginning of program. First call of this function: `initialized = false`, `srand` is called and `initlaized` becomes `true`. Second call or later: `initlaized` is still `true`, so no `srand` is called here. – MikeCAT Sep 25 '15 at 06:42
  • 2
    I think it's a bad idea to have the function seed the generator. That creates an undesirable coupling. For instance a caller cannot seed to reproduce a specific sequence. It's also wasteful to test `initialized` every time. I would very strongly recommend putting the requirement to seed on the caller. – David Heffernan Sep 25 '15 at 06:45
  • @MikeCAT, I removed the static variable to be global ( outside of the function and main), I still get good results – jarhead Sep 25 '15 at 06:48
  • @DavidHeffernan, how would you do that then? would you mind posting an example? – jarhead Sep 25 '15 at 06:48
  • 1
    @jarhead Remove everything related to `initialized` and instead call `srand` exactly once in `main`. Did you read the link that I gave in comments to the question? – David Heffernan Sep 25 '15 at 06:50
  • @DavidHeffernan, it also works well after removing initialized, Cheers – jarhead Sep 25 '15 at 06:53
  • Make sure you remember to seed though, in `main`, before the first call to `rand`. – David Heffernan Sep 25 '15 at 06:55