Home  >  Article  >  Backend Development  >  What does the srand(time(null)) function mean?

What does the srand(time(null)) function mean?

藏色散人
藏色散人Original
2019-06-06 11:02:3819386browse

What does the srand(time(null)) function mean?

The statement in C language srand( (time(NULL) ) ; means setting a random seed, and the random seed can be guaranteed to be different every time it is run.

In C language, the rand() function can be used to generate random numbers, but this is not a real random number, it is a pseudo-random number. It is based on a number. We can call it a seed, which is a benchmark. A coefficient calculated based on a certain recursive formula, but this is not a true random number. When the computer is turned on normally, the value of this seed is determined, unless you destroy the system in order to change the value of the seed.

Therefore, C provides the srand() function, whose prototype is void srand( int a). It is used to change this seed value.

srand( (time(NULL )) The time(NULL) function is to get the number of seconds from January 1, 1900 to the present, so that each time the program is run, different random numbers can be guaranteed.

Related tutorial recommendations: "C Video Tutorial"

The above is the detailed content of What does the srand(time(null)) function mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn