G-71
G-71

Reputation: 3682

How I can generate different random numbers in one moment in c++?

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

Answers (3)

BЈовић
BЈовић

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

BCoates
BCoates

Reputation: 1518

Move the srand outside of the while loop. You only need to call it once, rand() keeps static state.

Upvotes: 7

Xeo
Xeo

Reputation: 131789

You don't seed the random generator directly before you use it. Just use srand once before the loop.

Upvotes: 5

Related Questions