if we wish to generate a random number every time ,we use `srand(time(NULL));`

Why do we use time & NULL ? what does `This sets the “seed” of the random number generator.`

mean? what’s a seed?

can anyone explain this in an easier and simpler manner?

if we wish to generate a random number every time ,we use `srand(time(NULL));`

Why do we use time & NULL ? what does `This sets the “seed” of the random number generator.`

mean? what’s a seed?

can anyone explain this in an easier and simpler manner?

By themselves computers can’t generate truly random numbers, though they can get pretty close by using input from their environment.

In C++ the `rand()`

function generates a number based on a formula. The “Seed” is the number put into this formula. However if the seed is the same every time the program runs than the formula will output the same numbers every time. To get truly random numbers we need to use something, as a seed, that will change every time the program runs.

This is where `time()`

comes in. According to C documentation:

The value returned [ by time() ] generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC

Obviously this value will be constantly changing so it gives a semi random seed to the `rand()`

function every time the program is run.

As for `NULL`

, `rand()`

saves its value to the pointer specified in its first argument, since we aren’t saving its value, we use a `NULL`

pointer. I believe you’ll learn more about pointers later in the course.