Generate random numbers (Rand,srand usage) in C + +

Source: Internet
Author: User
Tags random seed

The random number of a computer is generated by a pseudo-random number, which is a sequence of small m-polynomial, in which each small sequence has an initial value, that is, a random seed. (Note: The period of the small m polynomial sequence is 65535, that is, the period of the random number generated by each random seed is 65535, and when you get 65,535 random numbers, they repeat.) )

We know that the rand () function can be used to generate random numbers, but this is not really a random number, it's a pseudo-random number, a series of numbers that are based on a recursive formula as a basis for a number (which we can call a seed), and when the series is large, it conforms to the normal publication, This is equivalent to generating a random number, but this is not a true random number, and the value of this seed is fixed when the computer is booted properly, unless you destroy the system.

1.rand ()

Function: Random number generator

Usage: int rand (void)

Header file: stdlib.h

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

RAND () returns the range of a random number between 0 and Rand_max. The range of Rand_max is at least between 32767 (int). With unsigned int DWORD is 65535, four bytes is an integer range of 4294967295. 0~rand_max the odds of each number being selected are the same.

When the user does not set a random number seed, the system default random number seed is 1.

RAND () produces pseudo-random numbers that are the same each time they are executed, and to be different, initialize it with the function srand ().

2.srand ()

Function: Initialize random number generator

Usage: void srand (unsigned int seed)

Header file: stdlib.h

The Srand () is used to set the random number seed when rand () produces a random number. The parameter seed must be an integer, and if each seed has the same value, the random number generated by RAND () will be the same every time.

3. Use the current clock as a random number seed

The random number generated by rand () is the same as the last time it was run. To be different, initialize it with the function srand (). You can use the Srand ((unsigned int) (Time (NULL)) method to produce different random number seeds, because each time you run the program is different.

4. Use of random number generation
1) provide a seed to Srand (), which is a 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 () as many times as necessary to obtain a new random number without interruption;
4) Whenever possible, a new seed can be given to Srand () to further "Randomize" the output of rand ().

A random number program between 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 [A, b], use (rand ()% (b-a)) + A;
To obtain a random integer [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 representation: A + (int) b * rand ()/(Rand_max + 1).
To obtain a floating-point number between 0~1, you can use RAND ()/double (Rand_max).

Generate random numbers (Rand,srand usage) in C + +

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: 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.