Explanation of srand (time (0) rand ()

Source: Internet
Author: User
Tags random seed
The computer cannot generate real random numbers. Algorithm Simulation, so you only call Rand, each time the output is the same. After a seed is set, different numbers can be generated based on different seed values. But how can we ensure the seeds are different? The simplest way is to always move forward. Srand (time (0); // set the seed first
Rand (); // then generate a random number. srand is the number of random seeds. The number of seeds you plant varies with each time. The random number obtained with Rand is different. Time (0) is used to get the current time value (because the time is different every moment) for each different seed ). Srand (time (0 ));

It is to give this algorithm a startup seed, that is, the random seed number of the algorithm. Only after this number is available can random numbers be generated,
Initialize the random number seed in seconds from 1970.1.1 to the present. Reference Cases
# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>

void main (void)
{< br> int I;
srand (time (0 ));
/* output 10 random numbers. */
for (I = 0; I <10; I ++)
printf ("% 6D/N", Rand ());
} output result: 22614
15772
30620
24913
7556
31659
27876
27229
25083
16564
press any key to continue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.