Generating random numbers (Rand,srand usage) in C + + __c++

Source: Internet
Author: User
Tags rand random seed


Random numbers of computers are generated by pseudo random numbers, which are produced by the small m polynomial sequence, in which each small sequence has an initial value, i.e. random seed. (Note: The cycle of a small m polynomial sequence is 65535, that is, the cycle of random numbers generated by a random seed each time is 65535, and they recur when you get 65,535 random numbers.) )

We know that the rand () function can be used to generate random numbers, but this is not really a random number, it is a pseudo-random number, is based on a number (we can call it the seed) as a reference to a recursive formula to calculate a series of numbers, when the series is very large, it is consistent with normal publication, This is equivalent to generating a random number, but this is not a real random number, and when the computer is turned on properly, the value of the seed is fixed, unless you destroy the system.

1.rand ()

Function: Random number generator

Usage: int rand (void)

The header file: stdlib.h

The internal implementation of RAND () is done with linear congruential, it is not a true random number, because its cycle is very long, so in a certain range can be seen as random.

RAND () returns a range of random values between 0 and Rand_max. The Rand_max range is at least between 32767 (int). The unsigned int double byte is 65535, and four bytes 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 the random number seed, the system default random number seed is 1.

Rand () produces a pseudo-random number that is the same every time it is executed, and, to be different, initializes it with a function srand ().

2.srand ()

Function: Initialization of random number generator

Usage: void srand (unsigned int seed)

The header file: stdlib.h

Srand () is used to set the random number seed for rand () to produce random numbers. The parameter seed must be an integer, and if each seed is set to the same value, the random values generated by rand () will be the same each time.

3. Use the current clock as a random number of seeds

The random number generated by rand () is the same as the previous one at each run. To be different, initialize it with a function srand (). The Srand ((unsigned int) (NULL) method can be used to produce different random number seeds because each time the program is run differently.

4. The use of generating random numbers
1) to provide a seed for srand (), which is a unsigned int type;
2 invokes Rand (), which returns a random number (between 0 and Rand_max) based on the seed value provided to Srand ();
3 Call rand () as many times as necessary to obtain new random numbers without interruption;
4 any time, you can give Srand () a new seed, thereby further "randomization" rand () output results.

Random number program between the 0~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 a certain range of random numbers
To obtain a random integer of [a,b], use (rand ()% (b-a)) + A;
To obtain a random integer of [a,b], use (rand ()% (b-a+1)) + A;
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 range of integers.
To get a random integer between A and B, another means: A + (int) b * rand ()/(Rand_max + 1).
To get floating-point numbers between 0~1, you can use the RAND ()/double (Rand_max).

Document Information

Author: A farmer (Www.cnblogs.com/afarmer)

Welcome to reprint, please keep the document information.

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.