IOS Srand (Time (0))

Source: Internet
Author: User

Reprint: http://blog.csdn.net/kongfanyu/article/details/6387642 Computer There is no way to generate a real random number, is to use algorithm simulation, so you only call Rand, every time out something is the same. After a seed is set, different numbers can be produced depending on the seed. And how do you keep the seeds different?  The simplest way of course is to use forever in the forward time. Srand (Time (0));//Set seed first
Rand ();//Then Generate random numbers Srand is a random number of seeds, you seed different seeds each time, the random number obtained by Rand is different. In order to plant a different seed every time, so choose time (0), when (0) is the current value (because the moment is different). Srand (Time (0)); is to give this algorithm a starting seed, that is, the random number of seeds of the algorithm, there is a number after this can produce random numbers,
Initializes the seed of the random number with the number of seconds since 1970.1.1. Reference Case#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void Main (void)
{
int i;

Srand (Time (0));

/* Output 10 random numbers. */
for (i = 0; I < 10;i++)
printf ("%6d/n", Rand ());
} Output: 22614
15772
30620
24913
7556
31659
27876
27229
25083
16564

IOS Srand (Time (0))

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.