# Random functions rand () and Srand ()

Source: Internet
Author: User
Tags random seed

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

First, Rand ()

Name of function: Rand
Function: Random number generator
Usage: int rand (void);

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 in a certain

Can be seen as random in the 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). Use

The unsigned int double byte is 65535, and four bytes is the integer range of 4294967295. 0~rand_max each number is selected

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

program Example:

1. #include <iostream>
2. Using namespace std;
3. #include <stdlib.h>
4. #include <time.h>
5. #define MIN 1//range generated by random numbers
6. #define MAX 10
7. int main ()
8. {
9. int i;
10. Srand ((unsigned) time (0));
11. cout<<"Ten random numbers from" <<MIN<<
12. "to" <<MAX<<":/n" <<endl;
13. For (i=0; i<10; i++) //Generate random numbers
14. {
15. Cout<<min + (int) MAX * rand ()/(Rand_max + 1) <<"/t";
16. }
17. cout<<endl;
18. return 0;
19. }

Second, Srand ()

Function Name: Srand
Function: Initialize random number generator
Usage: void srand (unsigned int seed);
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.

program Example:

1. #include <iostream>
2. Using namespace std;
3. #include <stdlib.h>
4. #include <time.h>
5. #define MIN 0//range generated by random numbers
6. #define MAX 99
7. int main ()
8. {
9. int i;
10. Srand ((unsigned) time (NULL));
11. cout<<"Ten random numbers from" <<MIN<<
12. "to" <<MAX<<":/n" <<endl;
13. For (i=0; i<10; i++) //Generate random numbers
14. {
15. Cout<<min + rand ()% (MAX + MIN-1) <<"/t";
16. }
17. cout<<endl;
18. return 0;
19. }

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.

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:

1. #include <iostream>
2. Using namespace std;
3. #include <stdlib.h>
4. #include <time.h>
5. int main ()
6. {
7. int i;
8. Srand ((unsigned) time (NULL)); //Initialize random number seed
9. For (i=0; i<10; i++) //Generate 10 random numbers
10. {
11. Cout<<rand () <<"/t";
12. }
13. cout<<endl;
14. return 0;
15. }

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

Random functions rand () and Srand ()

Related Keywords:

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.