Analysis of srand () and time (null) Usage

Source: Internet
Author: User
Tags random seed

In some productsSource codeSuch a statement is often found,

Srand (unsigned (Time (null )));

Why is this actually quite simple.

1. The time () function indicates the number of seconds from 00:00:00 to the current time. The time (null) function indicates obtaining a time. To be precise, it indicates obtaining a pointer address.

2. The srand () function generates random number seeds. When the random number rand () is called, he will check that if the user has called srand (SEED) before, he will call srand (SEED) again) to generate random seed. If srand (SEED) is not called, srand (1) is automatically called once. Therefore, if you want rand () to generate different values for each call, you need to call srand (SEED) once each time, and seed cannot be the same.

Based on the above two points, it is very clear.

Srand (unsigned (Time (null) indicates that random seed is generated to ensure that random values are not repeated when rand () is called.

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.