0

I'm using C++ on Visual Studio 2010. The problem is that I try to generate an array of random numbers, but the results are always the same on every run. For example:

Run 1:
[0 20103 43281 37162 101 299]
Stop
Run 2:
[0 20103 43281 37162 101 299]
Stop
...

So, my code is generating random values for the array, but they are always the same values everytime I run the code (even if I clean the build). I really need to generate random values for every run.

Here's some sample code, the complete code is full of simple operations:

...
srand(time(NULL));
for (int i = 0; i < 31; i++){
    x[i] = x[i] + rand();
}

Any ideas?

Please, note the presence of the srand(time(NULL)); line. It's already on the code, still the output is not fine.

Minoru
  • 1,680
  • 3
  • 20
  • 44
  • possible duplicate of [Why do I always get the same sequence of random numbers with rand()?](http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand) – olevegard Jan 09 '14 at 12:51
  • @olevegard: Question unrelated. His code clearly shows he is seeding the rng. – ctor Jan 09 '14 at 12:52
  • @ctor Ahh, sorry, I was too quick there – olevegard Jan 09 '14 at 12:54
  • 3
    @LucasHarada: Can you provide more sample code. There must be something else going on that has not been shown in your currently sample. I say this because even though you are adding the random numbers, some of those numbers decrease in value. – ctor Jan 09 '14 at 12:55
  • [testcase](http://sscce.org) please – Lightness Races in Orbit Jan 09 '14 at 13:02
  • @ctor In a first moment, I used the `rand()` to simulate a Gaussian Distribution variable (that is, I call the `rand()` twice). But when I saw the results were always the same on every run, I simplified it to the code above and it still have the same behavior. – Minoru Jan 09 '14 at 13:05
  • @ctor: Nothing wrong with a bit of unsigned wrap-around! – Lightness Races in Orbit Jan 09 '14 at 13:05
  • 1
    @LucasHarada: The "simplified code above" is not a valid C++ program, though. We can't feed it to a compiler as-is and see the behaviour you're talking about. I mean, we don't even know what `x` is! – Lightness Races in Orbit Jan 09 '14 at 13:05
  • These lines should produce randomly generated output. – SChepurin Jan 09 '14 at 13:08
  • -1 for not showing useful code. I will revise my vote if you edit your question to make it answerable. – JBentley Jan 09 '14 at 13:09
  • The complete code is about 500 lines of code. I'll try reduce it, but still let it compilable and post it here. One moment, please. – Minoru Jan 09 '14 at 13:11
  • 2
    @Lucas: You should have done that for your own debugging purposes before posting! That means when you said "I simplified it to the code above and it still have the same behavior", you were being dishonest. – Lightness Races in Orbit Jan 09 '14 at 13:12
  • @LightnessRacesinOrbit Not really. The code is still complete, I just swapped the main method to call most inner methods, without making the operations I need to do. The behaviour remained incorrect. – Minoru Jan 09 '14 at 13:14
  • @Lucas: The code above is not _500 lines_! – Lightness Races in Orbit Jan 09 '14 at 13:18
  • @LightnessRacesinOrbit: I was assuming a RAND_MAX of 65535 and x to be an array of int. – ctor Jan 09 '14 at 13:38
  • @ctor: Visible effects of overflow UB, then! – Lightness Races in Orbit Jan 09 '14 at 13:38

2 Answers2

1

Please, note the presence of the srand(time(NULL)); line. It's already on the code, still the output is not fine.

Seeding prepares a new pseudo-random sequence; rand calls advance you along that sequence. Every time you call seed with the same argument, you start that same sequence again.

Since your seed argument changes only once per second, if you run your program more than once per second, you will see the same pseudo-random sequence.

I would recommend seeding with a little more entropy, though how to do this is largely platform-dependent. On Linux I like to write srand(time(NULL) ^ getpid()).

Also remember to seed only once in your program (and it appears that you're getting this right).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • If your program is outputting the same pseudo-random sequence for runs that are more than one second apart, then this answer is incorrect, but it would then also be true that there is something crucial missing from your question. – Lightness Races in Orbit Jan 09 '14 at 13:04
0

Try randomizing like this:

int a;
a=rand()%*whatever range you want it to be*;

You can also change the srand(time(NULL));, because NULL means 0.

srand(time(1000));

It's unit is milliseconds, so 1000 is one second.

Hope it helps!