Srand (unsigned) time (NULL) and use and understanding of rand () functions, srandrand

Source: Internet
Author: User

Srand (unsigned) time (NULL) and use and understanding of rand () functions, srandrand

1. First, let's talk about the rand () function.

  The rand () function is equivalent to a random number generator. The generated number ranges from 0 to 32767 (Srand (seed) was not called previously)).

So why is the range 0?

0: srand (seed) is not called before the rand () function is used, srand (0) is called by default, and srand (seed) the seed in the function can be interpreted as the minimum value of a random number.

32767: The value is in hexadecimal notation 0x7fff, which is defined in the header file <stdlib. h>. define the macro: # define RAND_MAX 0x7fff.

That is to say, the maximum random value of rand () is fixed to 32767 (note: this number is not 2 ^ 16-1, not 65536, not the maximum value expressed by two-character energy saving)

That is to say, the use of the rand () function must contain the header file <stdlib. h>.

 

2. Let's talk about srand (seed );

Srand (seed) is equivalent to the declaration before rand () is used. Of course, if you do not declare it, the system will declare it to you by default. seed is the minimum value for generating a random number.

The following is an example code:

 1 #include <stdio.h> 2 #include <stdlib.h> 3  4 int main() 5 { 6     int a[10],i = 0; 7     srand(0); 8     for(;i < 10;i++) 9     {10         a[i] = rand();11         printf("%d,",a[i]);12     }13 }   

 

Here is the running result:

Generated is a number between 0 and ......

 

However, using this function is strictly not random, but random under certain rules.

For example, when I run this function for the second time:

You will find that each time you run the array, the values are exactly the same, but the values in the array are random.

If you determine a seed value, for example, 0 in the above Code, the random values of each program run will be the same, that is, the second time I run the above program, the obtained random value is the same as the random value obtained during the first running.

So how can we truly obtain random values? Therefore, the seed value in the srand (seed) of the program must be different each time, and the obtained array value is different, thus realizing the real random number.

This requires the time function.

 

3. Declare srand (unsigned) time (NULL) to implement true random

First, the time () function is used to obtain the time elapsed since 00:00:00, January 1, January 1, 1970, in seconds. It is included in the header file <time. h>

Because time passes by, so this function is used as the seed value, so the value of each seed is different, rand () is truly random.

Because we do not need parameters to record the passed value, we generally use time (NULL) and forcibly convert it to the unsigned type.

It becomes srand (unsigned) time (NULL )).......................

Change srand (0) in the above Code to srand (unsigned) time (NULL), and add the header file <time. h>, the program runs as follows:

First time:

Second:

In this way, we get different values each time we run them.

Related Article

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.