-3

what does the line srand(time(NULL)) do in the following code to generate random numbers? what does time mean here?

#include <stdio.h>  
#include <stdlib.h>
#include <time.h>
int main ()
{
    int iSecret, iGuess;
    srand (time(NULL));

    iSecret = rand() % 10 + 1;

    do {
        printf ("Guess the number (1 to 10): ");
        scanf ("%d", &iGuess);
        if (iSecret < iGuess) 
            puts ("The secret number is lower");
        else if (iSecret > iGuess) 
            puts ("The secret number is higher");
    } while (iSecret != iGuess);
    puts ("Congratulations!");
    return 0;
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
aditya
  • 165
  • 2
  • 12
  • Did you try [Google](https://www.google.com/search?q=c+time)? The first hit answers your question (assuming that C++ tag is accurate). – user2357112 Jul 30 '14 at 09:34
  • you are making your life worse later if you don't learn how to properly indent and format your code. – EpicPandaForce Jul 30 '14 at 09:53
  • You might like [my answer to a similar question](http://stackoverflow.com/a/15571186/25324) posted some months ago. – pmg Jul 30 '14 at 10:27
  • possible duplicate of [in C, how does srand relate to rand function?](http://stackoverflow.com/questions/21273550/in-c-how-does-srand-relate-to-rand-function) – mc110 Jul 30 '14 at 11:25

6 Answers6

1

Random number are indeed pseudo-random number.

The generation mechanism needs a seed. So has not to take the same seed everytime ( which would produce always the same sequence of random numbers), if you take a seed linked to time then you always have a new seed, which allows generating different random numbers.

srand (time(NULL)); does extacly this.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
1

The srand function seeds the pseudorandom number generator, which means that for the same seed in srand(seed); you'll always get the same sequence of random numbers.

But when you use fixed seed, this would get you always the same random numbers sequence. So to help the ilusion of "randomness", you can set the seed to a a value, which would be different everytime you run the program. And using time(NULL) is simplest way of providing different seeds for every run.

Jan Spurny
  • 5,219
  • 1
  • 33
  • 47
1

When you didn't use srand() for random number generation, whenever you compile your program you will get the same number as output! here srand() is a seed to the rand().

srand (time(NULL)); 

Normally time is not a stable one in the world, so it will change for every second! so that time it will give updated time in seconds as a seed. so you will get different random number as a output!

But when you use time() as a seed, you may get the same number as a output when you run your program multiple times with in a second, after every second time only it give different output. To avoid this error use the following way-

srand (getpid()); // Best one

When ever you run your program, the process id will be different! so in this method you never get the same number, if you run your program multiple times with in a second!

Sathish
  • 3,740
  • 1
  • 17
  • 28
0

It seeds the random number generator, as it is completely deterministic and would give the same value every time unless seeded.

time is the standard C function that returns the current time elapsed since epoch.

As this post is tagged C++ I would suggest not using these functions and investing time into the pseudo-random non-deterministic random number generators available to you

RamblingMad
  • 5,332
  • 2
  • 24
  • 48
0

As you can find in the reference for the srand function, it is used to initialize the pseudo-random number generator starting from the seed you pass as argument. If you pass a null pointer (NULL) to the time function it only returns the current calendar time, and this is why it is used to generate the seed for the time function. In fact the current time varies frequently.

Enry_h2o
  • 31
  • 1
  • 1
0

As per details in link What is time(NULL) in C?

You can pass in a pointer to a time_t object that time will fill up with the current time (and the return value is the same one that you pointed to). If you pass in NULL, it just ignores it and merely returns a new time_t object that represents the current time.

time(NULL) simply returns the current time details.

srand() is used for generating a random number.

srand(time(NULL)) just creates a random number, using the current time details as input.

Community
  • 1
  • 1
rohit
  • 198
  • 1
  • 1
  • 7