-5

Why do we use 'NULL' in code below.

Why can't we multiply the seed by an integer?

Sorry I am new to C++.

CODE

srand(time(NULL));
elcuco
  • 8,948
  • 9
  • 47
  • 69
Shahroz
  • 49
  • 9
  • 1
    https://linux.die.net/man/2/time -> _If t is non-NULL, the return value is also stored in the memory pointed to by t._ – Amadeus Sep 29 '19 at 13:50
  • Lets reopen - the answer is not "what s null", but "why should we use null" ? – elcuco Sep 29 '19 at 13:55
  • 1
    @elcuco why to reopen? To me, the linked answer is clear to why there is a need to pass NULL – Amadeus Sep 29 '19 at 13:59
  • 1
    I think the answer and duplicate both explain why NULL is used very well. – drescherjm Sep 29 '19 at 13:59
  • @elcuco as I said on first comment, I think there is a lack of effort. The question is very clear _Why do we use 'NULL' in code below._ and the answer is clear too. Any futher questions could be easily solved by 10 minutes of search on internet, for example: https://linux.die.net/man/3/srand – Amadeus Sep 29 '19 at 14:10

1 Answers1

4

The time function can write the time to a location provided by a pointer to the function call. This pointer argument can be a null pointer, and then time only returns the current time.

On most systems the time function returns the number of seconds since an epoch, and as such is a pretty unique integer value to use for seeding the random number generator.


The single statement

srand(time(NULL));

is equivalent to

time_t temp_time = time(NULL);
srand(temp_time);

Or if we want to use a non-null pointer

time_t temp_time;
time(&temp_time);
srand(temp_time);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621