0

Possible Duplicate:
Recommended way to initialize srand?

I have the following problem when using srand() in c.

I call srand(time(NULL)) in a loop, but the loop finishes before 1 sec and every time I call the rand() I get the same value.

How can I solve this?

Community
  • 1
  • 1
phedon rousou
  • 1,740
  • 5
  • 17
  • 24
  • 1
    why are you calling `srand()` in a loop? You just call it once, then use `rand()` – effeffe Nov 22 '12 at 21:06
  • 2
    Call srand() only once at the start of your program. – Stefan Fandler Nov 22 '12 at 21:06
  • I am trying to do password generation using srand(time()) and srand(num), so, I override the srand() every time an iteration is done. That's is why I have the srand() in the loop. It gets changed twice for every iteration – phedon rousou Nov 22 '12 at 21:41

6 Answers6

1

Why are you calling srand in a loop? Just call it once at the start of your program and then call rand any number of times.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Because I have 2 srand() in the loop for two different purposes. One uses time() and the other one a randomSeed. To generate the random seed I need the srand with time. – phedon rousou Nov 22 '12 at 21:43
0

Don't call srand in the loop. Why are you doing this?

Just initialize it outside the loop, once.

Jonatan Hedborg
  • 4,382
  • 21
  • 30
  • Because I have 2 srand() in the loop for two different purposes. One uses time() and the other one a randomSeed. To generate the random seed I need the srand with time. – phedon rousou just now edit – phedon rousou Nov 22 '12 at 21:44
  • If you need more than one random number generator, I would recommend using one from a 3rd party library instead, and make several instances of it. Rand isn't very good anyway... – Jonatan Hedborg Nov 22 '12 at 22:19
0

You just need to initialize srand() once, then you just need to use rand() to generate the random numbers. And to generate random numbers use Better random algorithm?

If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in

j = 1 + (int) ( 10.0 * ( rand() / ( RAND_MAX + 1.0 ) ) );
Community
  • 1
  • 1
Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93
0

srand's purpose is to initialize the random number generator.

Its parameter is called a seed. If you give the same seed twice, you can expect the random number generator (subsequent calls to rand()) to return the same sequence of "random" numbers.

In your case you're constantly calling srand with the same value (until the second changes), so rand() will always return you the same value.

You just need to call srand once.

f4.
  • 3,814
  • 1
  • 23
  • 30
0

because the seed is bound into the time() which are seconds since unix epoch, basically you're giving it the same seed because the loop takes less than a second.

What you should do is get the time in microseconds. Take a look at gettimeofday() if you're coding for windows google microseconds win32 C, you mind need to convert it from double to integerso just do this (unsigned int)double * 100.0f;

  • struct timeval tv; gettimeofday(&tv,NULL); unsigned long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec;//find the microseconds for seeding srand() – phedon rousou Nov 22 '12 at 21:45
0

I found the answer with your help.

        struct timeval tv;
        gettimeofday(&tv,NULL);
        unsigned long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec;//find the microseconds for seeding srand()
        srand(time_in_micros);
phedon rousou
  • 1,740
  • 5
  • 17
  • 24