Usage of the random function rand () and Srand () in C + + __jquery

Source: Internet
Author: User
Tags generator rand random seed

I. rand ()

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

The header file: stdlib.h

Function Description:

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

Can be seen as random in its scope.

RAND () returns a range of random values between 0 and Rand_max. The Rand_max range is at least between 32767 (int). Use

unsigned int double-byte is 65535, four bytes is a 4294967295 integer range. 0~rand_max each number is selected

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

program Example:

#include <iostream> using namespace std; #include <stdlib.h> #include <time.h> #define MIN 1//random number produced range #define MAX int main () {int i; Srand (unsig Ned) Time (0)); cout<< "Ten random numbers from" <<MIN<< "to" <<MAX<< ":/n" <<endl; For (i=0 i<10; i++)//Generate random number {cout<<min + (int) MAX * rand ()/(Rand_max + 1) << "/T";} cout<<endl; return 0; }

Second, Srand ()

Function Name: Srand
Function: Initialization of random number generator
Usage: void srand (unsigned int seed);
The header file: stdlib.h
Function Description:
Srand () is used to set the random number seed for rand () to produce random numbers.

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

If each seed is set to the same value, the random values generated by rand () will be the same each time.

program Example:
#include <iostream> using namespace std; #include <stdlib.h> #include <time.h> #define MIN 0//random number produced range #define MAX int () {int i; Srand (unsig Ned) Time (NULL)); cout<< "Ten random numbers from" <<MIN<< "to" <<MAX<< ":/n" <<endl; For (i=0 i<10; i++)//Generate random number {cout<<min + rand ()% (MAX + MIN-1) << "/T";} cout<<endl; return 0; }

iii. Relationship between Rand () and Srand ()

Rand () and Srand () are used together, where Srand () is used to initialize the random number seed, and rand () is used to produce random numbers.

Because the random number seed is 1 by default, and the same random number produces random numbers that are the same and lose the meaning of randomness, the random number of seeds is initialized with function Srand () for each random number obtained. Srand () parameter, with the time function value (that is, the current time), because two times the rand () function is usually different, so that the randomness is guaranteed.

general formula for generating a certain range of random numbers

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

(Overall, general formula: A + rand ()% n; where a is the starting value, 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).

v. Causes of the same random number

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 that's not true. A random number in the sense of a pseudo random number is a series of numbers calculated by a recursive formula based on a number (we can call it a seed), and when the series is large, it conforms to a normal announcement, 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.

program Example:

#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<<ran D () << "/t"; } cout<<endl; 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)) before starting to generate random numbers (note: Srand () must be placed outside the loop or outside the loop, otherwise the same random number is obtained).

program Example:

#include <iostream> using namespace std; #include <stdlib.h> #include <time.h> int main () {int i; Srand ((unsigned) time (NULL));//Initialize the random number seed for (i=0; i& lt;10; i++)//generates 10 random numbers {Cout<<rand () << "T";} cout<<endl; 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

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.