Chapter 2: Storage Class specifiers and random numbers

Source: Internet
Author: User
Tags define function random seed

Storage Class description:

Auto:It indicates that a variable has an automatic storage period and can only be used in variable declaration of code block scope. It is mainly used to indicate the intention and make the program easier to read.

Register:It andAutoThe specifiers can only be used in variable declaration within the scope of a code block. A variable is classified into a register storage class, that is, a variable is changed to a register and accessed faster. Because the variables are stored in registers, the address of the variables cannot be obtained.

Static:It can be used to declare the code block scope or to declare the File Scope. When it is used to declare the code block scope, the variable has a static storage period (that is, it exists after the declaration and retains its value) and empty links. When it is used to declare the file scope, it indicates that the variable has an internal link, and its storage period is static storage period.


Random Number: Rand and srand (Srand initializes random seeds, and Rand generates random numbers.)

Rand :(Generate random number)

Header file: # include <stdlib. h>

Define function: int rand (void)

Function Description:
Because the internal implementation of Rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, Rand () returns a random value ranging from 0 to rand_max. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, Rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().

Return Value:
Returns a random integer between 0 and rand_max. The range of rand_max is at least 32767 (INT), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ Rand_max the probability of each number being selected is the same

Srand:(Set Random Seed)
Header file: # include <stdlib. h>

Define the function: void srand (unsigned int seed );

Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

In addition, the srand function must be placed outside the loop or called cyclically. Otherwise, the same number is obtained.

Summary:
The rand () function can be used to generate random numbers, but this is not a real random number. It is a pseudo-random number Based on a number (it can be called a seed) it is a coefficient calculated based on a recursive formula. When this coefficient is large, it complies with the normal announcement, which is equivalent to generating a random number, but this is not a real random number, after the computer starts up normally, the seed value is fixed. Unless you break the system, C provides the srand () function to change the seed value, its prototype is void srand (int A). It initializes the initial values of both the rand () function of the random generator, even if the seed value is changed to; from this you can see that through the sand () function, we can generate predictable random sequences. How can we generate Unexpected random sequences?

Srand (unsign) (Time (null) is a method, because the time of each running program is different



Zookeeper

Chapter 2: Storage Class specifiers and random numbers

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.