Usage of Random Functions rand () and srand () in C ++

Source: Internet
Author: User
Tags random seed

I. Rand ()

Function Name: Rand
Function: random number generator
Usage: int rand (void );

Header file: stdlib. h

Function Description:

The internal implementation of rand () is done by the linear same remainder method. It is not a real random number. Because of its long cycle

Can be seen as random.

Rand () returns a random value ranging from 0 to rand_max. The range of rand_max is at least 32767 (INT ). Use

Unsigned int dual-byte is 65535, and four-byte is an integer range of 4294967295. 0 ~ Rand_max: Each number is selected.

The probability 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.

Program example:

# Include <iostream> <br/> using namespace STD; <br/> # include <stdlib. h> <br/> # include <time. h> <br/> # define min 1 // range of Random Number Generation <br/> # define max 10 </P> <p> int main () <br/>{< br/> int I; <br/> srand (unsigned) time (0 )); <br/> cout <"ten random numbers from" <min <br/> "to" <max <":/N" <Endl; <br/> for (I = 0; I <10; I ++) // generates a random number <br/>{< br/> cout <min + (INT) max * rand ()/(rand_max + 1) <"/t"; <br/>}< br/> cout <Endl; <br/> return 0; <br/>}

Ii. srand ()

Function Name: srand
Function: Initialize the random number generator.
Usage: void srand (unsigned int seed );
Header file: stdlib. h
Function Description:
Srand () is used to set the random number seed when rand () generates a random number.

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

If the same value is set for each seed, the random values generated by rand () are the same each time.

Program example:
# Include <iostream> <br/> using namespace STD; <br/> # include <stdlib. h> <br/> # include <time. h> <br/> # define min 0 // range of Random Number Generation <br/> # define Max 99 </P> <p> int main () <br/>{< br/> int I; <br/> srand (unsigned) Time (null )); <br/> cout <"ten random numbers from" <min <br/> "to" <max <":/N" <Endl; <br/> for (I = 0; I <10; I ++) // generates a random number <br/> {<br/> cout <min + rand () % (MAX + min-1) <"/t"; <br/>}< br/> cout <Endl; <br/> return 0; <br/>}

3. Relationship between rand () and srand ()

Rand () and srand () must be used together. srand () is used to initialize the random number seed, and rand () is used to generate a random number.

By default, the random number seed is 1, and the random number generated by the same random number seed is the same, thus the random number obtained each time is different, use the srand () function to initialize the Random Seed. The srand () parameter uses the time function value (that is, the current time), because the time for two calls to the rand () function is usually different, so as to ensure randomness.

4. General expression formula for generating random numbers within a certain range

To obtain a random integer of [a, B), use (RAND () % (B-a) + a (the result value contains a and does not contain B ).
To obtain a random integer of [a, B], use (RAND () % (B-A + 1) + a (the result value includes a and B ).
To obtain a random INTEGER (a, B], use (RAND () % (B-a) + A + 1 (the result value does not include a and B ).

(In general, the general formula is: 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 ).

5. Reasons for generating the same random number

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.

Program example:

# Include <iostream> <br/> using namespace STD; <br/> # include <stdlib. h> <br/> # include <time. h> </P> <p> int main () <br/>{< br/> int I; <br/> for (I = 0; I <10; I ++) // generates 10 random numbers <br/>{< br/> cout <rand () <"/t "; <br/>}< br/> cout <Endl; <br/> return 0; <br/>}

The same random sequence is obtained each time:

41 18467 6334 26500 19169 15724
41 18467 6334 26500 19169 15724
29358 26962 24464

To obtain different random number sequences, you need to change the value of this seed. Method: Before a random number is generated, call srand (Time (null) (Note: srand () must be placed out of the loop or out of the loop, otherwise, the same random number is obtained ).

Program example:

# Include <iostream> <br/> using namespace STD; <br/> # include <stdlib. h> <br/> # include <time. h> </P> <p> int main () <br/>{< br/> int I; <br/> srand (unsigned) Time (null )); // initialize Random Seed <br/> for (I = 0; I <10; I ++) // generate 10 random numbers <br/>{< br/> cout <rand () <"/t "; <br/>}< br/> cout <Endl; <br/> return 0; <br/>}

Different random sequences are obtained each time:

1294 18562 14141 18165 11910 29784 11070 13225 131 24405
1774 25714 18734 16528 20825 17189 9848 8899 2503 5375

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.