0

Possible Duplicate:
how to generate different random number in a loop in C++?

This is my code:

 for(i=0;i<10;i++)
 {
    srand ( time(NULL) );
    cout<<time(NULL);
    max=100,min=0;
    for(j=0;j<3;j++)
    {
          cout<<(( (rand() % (max - min + 1)) + min)%5);
    }
 }

Now i get the output:

1357207288 0 1 4

1357207289 0 1 4

1357207290 0 1 4

and so on. I want to get different random numbers each time. How can I achieve this.

Community
  • 1
  • 1
arjun
  • 2,333
  • 4
  • 21
  • 21
  • 3
    Move the call to `srand()` out of the loop. –  Jan 03 '13 at 10:05
  • @LuchianGrigore And who cares? I bet 3 calls to `rand()` don't take up one second... –  Jan 03 '13 at 10:07
  • I have tried moving srand() out of the loop. But i get the same result. At each iteration of outer loop i want different set of random numbers – arjun Jan 03 '13 at 10:08
  • @H2CO3 ah, true, the output is "1357207288 0 1 4" nevermind... :D – Luchian Grigore Jan 03 '13 at 10:08
  • 3
    Since you say you are using C++, you could take a look at the new [pseudo random functionality](http://en.cppreference.com/w/cpp/numeric/random) of C++11, especially the class [`std::uniform_int_distribution`](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) which can be used to generate random numbers in a range. There is an example on how to use it in the link. – Some programmer dude Jan 03 '13 at 10:10
  • I am starting to suspect some dirty tricks by the compiler - it thinks a function is without side-effects when it isn't and such... – John Dvorak Jan 03 '13 at 10:20
  • 2
    @arjun You have tried moving `srand` out of the loop and nothing changed? I find that hard to believe. Could you check: 1) Have you _removed_ `srand` from inside the loop and _instead_ inserted it before the loop, 2) Have you actually recompiled your program after making the change? – jogojapan Jan 03 '13 at 10:20
  • 2
    @jogojapan In the end I find it hard to believe `srand` being in the loop to be the error, since he puts it out, too, and it is a different number each time (given that he shows the correct output, which might not be the case seeing that his code misses the spaces present in his output, hmmm). – Christian Rau Jan 03 '13 at 10:23
  • @ChristianRau great eye. Now to the point - arjun, what is the _real_ code you're dealing with? – John Dvorak Jan 03 '13 at 10:24
  • @ChristianRau Yes, I noticed this. Then again, that code above isn't the code that was used to generate the output, because otherwise there wouldn't be spaces between the numbers, not to mention newlines. When the output was interpreted, and then adjusted for SO, all kinds of things may have gone wrong. – jogojapan Jan 03 '13 at 10:25
  • I don't believe this is your real code (it cannot possibly generate that output, no whitespace!). Please post a **complete** test-case. – Oliver Charlesworth Jan 03 '13 at 10:25
  • 2
    For future reference, when having code and output from that code in a post, please make it the _exact_ code that produces the output. Also please read http://sscce.org/. – Some programmer dude Jan 03 '13 at 10:26

4 Answers4

8

The reason you get the same repeatedly is because you initialize it with the same seed each time. That is, since you are performing so few operations, not a single second has passed since the first iteration of the loop to the last one. Hence, time(nullptr) will return the same for each iteration.

To solve this, move srand ( time(NULL) ); outside of the loop, which will mean setting the random seed only once.

 srand ( time(NULL) );
 for(int i=0;i<10;i++)
 {
    cout<<time(NULL);
    int max=100;
    for(int j=0;j<3;j++)
    {
          cout<<(( (rand() % (max - min + 1)) + min)%5);
    }
 }
Agentlien
  • 4,996
  • 1
  • 16
  • 27
  • I tried the above code. It still yields the same result as before. – arjun Jan 03 '13 at 10:10
  • Are you sure he gets the same time every iteration (because he puts it out and the numbers look different to me)? – Christian Rau Jan 03 '13 at 10:21
  • @ChristianRau That's a valid point. In his example data, there is a +1 second increase between calls. The interesting question is: Is his code and data copy-pasted or recreated while writing the post? – Agentlien Jan 03 '13 at 10:23
  • IMHO the output doesn't seem to be generated from that code – Gianluca Ghettini Jan 03 '13 at 10:24
  • 1
    @Agentlien In the end his code misses the spaces present in his output, which makes me believe he didn't even show the correct output, so you might be right with your answer. – Christian Rau Jan 03 '13 at 10:25
3

You must move srand() outside of your loop, otherwise you receive the same numbers.

srand ( time(NULL) );
for(i=0;i<10;i++)
{
cout<<time(NULL);
max=100,
for(j=0;j<3;j++)
{
      cout<<(( (rand() % (max - min + 1)) + min)%5);
}
}

If you don't you will use the same seed since the time will not change in the nanoseconds your program takes to execute.

Component 10
  • 10,247
  • 7
  • 47
  • 64
1
srand(time(NULL));
printf("%d", rand() % 10+1);
for(i=1; i<rand()% max_length; i++) {
  printf("%ld", rand() % 10);
}

This will generate random numbers (also random length of them).

Maciej Małecki
  • 2,725
  • 19
  • 29
1

You get the same results even with srand() out of the loop because in C the generation algorithm used by rand is guaranteed to only be advanced by calls to this function. In C++, this constraint is relaxed, and a library implementation is allowed to advance the generator on other circumstances (such as calls to elements of <random>). Put a sleep inside the loop and see what happens.

Obviously in plain C this doesn't happen. However, boost libraries offers you some good PRNG functionality. Use it instead of the broken srand(time(NULL));

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159