Use of the Random function rand () and Srand () in C + +

Source: Internet
Author: User
Tags random seed

C + + Tutorials This article tells you about the use of the random functions rand () and Srand () in C + +!

First, Rand ()

Name of function: Rand

Function: Random number generator

Usage: int rand (void);

Header file: stdlib.h

Function Description:

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 ().

Examples of programs:

#include<iostream>

using namespace Std;

#include<stdlib.h>

#include<time.h>

#define MIN 1//range generated by random numbers

#define MAX 10

int main ()

{

int i;

Srand ((unsigned) time (0));

cout<< "Ten random numbers from" <

"To" <

for (i=0; i<10; i++)//Generate random numbers

{

cout<

}

cout<

return 0;

}

Second, Srand ()

Function Name: Srand

Function: Initialize random number generator

Usage: void srand (unsigned int seed);

Header file: stdlib.h

Function Description:

The Srand () is used to set the random number seed when rand () produces a random number.

The parameter seed must be an integer and can usually be used as seed with the return value of time (0) or null.

If each seed is set to the same value, rand () produces a random number that is the same every time.

Examples of programs:

#include<iostream>

using namespace Std;

#include <stdlib.h>

#include<time.h>

#define MIN 0//range generated by random numbers

#define MAX 99

int main ()

{

int i;

Srand ((unsigned) time (NULL));

cout<< "Ten random numbers from" <

"To" <

for (i=0; i<10; i++)//Generate random numbers

{

cout<

}

cout<

return 0;

}

Iii. the relationship between Rand () and Srand ()

Rand () and Srand () are used together, where Srand () is used to initialize a random number seed, and rand () is used to generate a random number.

Because the random number seed is 1 by default, and the random number generated by the same random number is the same, it loses the meaning of randomness, so the random number seed is initialized with the function srand () to make the random number different each time it gets. The Srand () parameter, with the time function value (that is, the current time), since two calls to the rand () function are usually different, thus guaranteeing randomness.

Iv. general expression formulae for generating a certain range of random numbers

To obtain a random integer of [a, b], use (rand ()% (b-a)) + A (the result value contains a does not contain a).

To obtain a random integer [A, b], use (rand ()% (b-a+1)) + A (the resulting value contains a and a).

To obtain a random integer (A, b], use (rand ()% (b-a)) + A + 1 (the resulting value does not contain a containing a).

(Overall, 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).

V. Reasons for generating the same random number

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 a random number in the true sense, a pseudo-random number, a series of numbers derived from a recursive formula based on a number (which we can call a seed), and when the series number 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.

Examples of programs:

#include <iostream>

using namespace Std;

#include <stdlib.h>

#include<time.h>

int main ()

{

int i;

for (i=0; i<10; i++)//generate 10 random numbers

{

cout<

}

cout<

return 0;

}

Each run gets the same random sequence:

41 18467 6334 26500 19169 15724 11478

41 18467 6334 26500 19169 15724 11478 29358 26962 24464

To get a different sequence of random numbers, you need to change the value of this seed. Method: Call Srand (Time (NULL)) once before starting to generate a random number (note: Srand () must be placed outside the loop or outside the loop call, otherwise the same random number is obtained).

Examples of programs:

#include<iostream>

using namespace Std;

#include<stdlib.h>

#include<time.h>

int main ()

{

int i;

Srand ((unsigned) time (NULL)); Initialize random number Seed

for (i=0; i<10; i++)//generate 10 random numbers

{

cout<

}

cout<

return 0;

}

Each run gets a different random sequence:

1294 18562 14141 18165 11910 29784 11070 13225 131 24405

1774 25714 18734 16528 20825 17189 9848 8899 2503 5375


Use of the Random function rand () and Srand () 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: 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.