Random not random 2nd time?


#1

When I run the random possessions program multiple times, I get the same word combinations/serial nums/dollar values. Is this normal? I expected it to be different each time.


#2

I noticed that myself. Even recompiling with small changes to the code, the results stay the same.


#3

Computer generated random numbers are kind of strange. They start with a “seed” number that gets its bits twiddled when a new random number is generated. This process forms a pattern; in fact, if you know the seed number, you know every random number that will be generated and in what order they are generated in.

The seed number on launching an application is always 0, so you always see the same pattern. If you want to change the seed, at launch, you can do this:

srandom(time(NULL)); (if using random() to generate random numbers)

or

srand(time(NULL)); (if using rand() to generate random numbers)

This probably throws a warning for type conversions, so cast appropriately.