Reputation: 3682
How I can generate different random numbers in one moment in c++?
Now I use
while ( flag )
{
....
srand( time( NULL) );
int rndSig = rand() % 10 + 1;
......
}
But at one moment all numbers are equal. How I can generate different numbers in while loop?
Upvotes: 2
Views: 480
Reputation: 64223
Like this :
srand( time( NULL) );
while ( flag )
{
....
int rndSig = rand() % 10 + 1;
......
}
If you need different distribution, or better randomness, you need to use a library.
Upvotes: 4
Reputation: 1518
Move the srand outside of the while loop. You only need to call it once, rand() keeps static state.
Upvotes: 7
Reputation: 131789
You don't seed the random generator directly before you use it. Just use srand
once before the loop.
Upvotes: 5