You can use the Srand () function and the rand () function to generate a random number, 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.
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 ().
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.
Here's a chestnut:
#include <iostream>#include<cstdlib>#include<ctime>usingstd::cout;#defineMIN 1//the range generated by the random number#defineMAX 10intMain () {inti; Srand ((unsigned) time (0)); cout<<"Ten random numbers from"<<MIN<<" to"<<MAX<<": \ n"<<Endl; for(i=0; i<Ten; i++)//Generate random numbers{cout<<min + (int) MAX * RAND ()/(Rand_max +1) <<"\ t"; } cout<<Endl; return 0; }
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).
Generate a random number in C + +