Introduction and usage of random number generation functions in C/C ++

Source: Internet
Author: User

Transferred from: Http://zhangjunhd.blog.51cto.com/113473/197020 A computer's random number is generated by a pseudo-random number, that is, a small M polynomial sequence. Each small sequence column is generated with an initial value, that is, a random seed. (Note: the period of a small M polynomial sequence is 65535, that is, the period of a random number generated by a random seed is 65535. When you obtain 65535 random numbers, they repeat again .) We know that 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 (we can call it a seed) A series of numbers calculated based on a recursive formula. When the number of series is large, it is in line with the normal announcement, which is equivalent to generating a random number, but this is not a real random number, after the computer is properly started, the seed value is fixed unless you break the system. 1. rand () function: random number generator usage: int rand (void) header file: stdlib. the internal implementation of H rand () is done by the linear coremainder method. It is not a real random number. Because of its long cycle, it can be regarded as random within a certain range. Rand () returns a random value ranging from 0 to rand_max. The range of rand_max is at least 32767 (INT ). The unsigned int double byte is 65535, and the four byte is an integer range of 4294967295. 0 ~ Rand_max the probability of each number being selected is the same. When the user does not set a random number seed, the default random number seed is 1. Rand () generates pseudo-random numbers, which are the same during each execution. to be different, use the srand () function to initialize it. 2. srand () function: Initialize the random number generator usage: void srand (unsigned int seed) header file: stdlib. h srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. If the same value is set for each seed, the random values generated by rand () are the same each time. 3. the random number generated by using the current clock as the random number seed rand () is the same as the previous one at each run. To be different, use the srand () function to initialize it. The srand (unsigned INT) (Time (null) method can be used to generate different random number seeds, because each running Program The time is different. 4. Usage of Random Number Generation
1) provide a seed for srand (), which is an unsigned int type;
2) call Rand (), which returns a random number (between 0 and rand_max) based on the seed value provided to srand );
3) call Rand () multiple times as needed to continuously obtain new random numbers;
4) at any time, srand () can be provided with a new seed to further "randomize" the output results of rand. 0 ~ Random Number program between rand_max # include <iostream>  
# Include <stdlib. h>  
# Include <time. h>

Using   Namespace   STD;

Int   Main ()  
{
Srand (unsigned) Time (null ));
For ( Int   I = 0; I <10; I ++)
Cout <rand () <'\ T ';
Cout <Endl;
Return   0;  
} 5. General expression formula for generating random numbers within a certain range
To obtain a random integer of [a, B), use (RAND () % (B-a) +;
To obtain a random integer of [a, B], use (RAND () % (B-A + 1) +;
To obtain a random INTEGER (a, B], use (RAND () % (B-a) + A + 1;
General Formula: A + rand () % N; where a is the starting value and N is the Integer Range.
To obtain a random integer between A and B, A + (INT) B * rand ()/(rand_max + 1 ).
0 ~ The floating point between 1. You can use rand ()/double (rand_max ). The above content is organized from Internet information.

This article is from the "sub-blog", please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/197020

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.