A detailed explanation of the random functions rand of and srand of in C++

  • 2020-05-12 02:55:33
  • OfStack

1. rand()

Function name: rand

Power: random number generator

How to use: int rand(void);

The header file: stdlib.h

Function description:

The internal implementation of rand() is done using the linear congruence method. It is not really a random number, because it has a very long period, so it is fixed at 1

It can be regarded as random.

rand() returns 1 random value between 0 and RAND_MAX. The range of RAND_MAX is at least between 32767 (int). with

unsigned int double bytes are 65535, and 4 bytes are in the integer range of 4294967295. 0~RAND_MAX each number is selected

The probability is the same.

When the user does not set the random number seed, the default random number seed of the system is 1.

rand() generates a pseudo-random number that is the same every time it is executed; To make a difference, initialize it with the function srand().

Examples are as follows:


#include <iostream> 
using namespace std; 
#include  <stdlib.h>   
#include  <time.h>  
#define MIN 1  // Range of random number generation    
#define MAX 10 
 
int main()   
{  
  int i;  
  srand((unsigned)time(0)); 
  cout<<"Ten random numbers from "<<MIN<< 
     " to "<<MAX<<" :/n"<<endl;   
  for(i=0; i<10; i++)     // Generate a random number  
  { 
    cout<<MIN + (int)MAX * rand() / (RAND_MAX + 1)<<"/t";  
  } 
  cout<<endl; 
  return  0;   
}  

2. srand()

Function name: srand

Power: initializes a random number generator

Usage: void srand(unsigned int seed);

Location header: stdlib.h

Function description:

srand() is used to set the seed of a random number when rand() generates a random number.

The parameter seed must be an integer, and you can usually use the return value of time(0) or NULL as seed.

If seed is set to the same value every time, rand() will generate a random value one time.

Examples are as follows:



#include <iostream> 
using namespace std; 
#include  <stdlib.h>   
#include  <time.h>  
#define MIN 0  // Range of random number generation    
#define MAX 99 
 
int main()   
{  
  int i;  
  srand((unsigned)time(NULL)); 
  cout<<"Ten random numbers from "<<MIN<< 
     " to "<<MAX<<" :/n"<<endl;   
  for(i=0; i<10; i++)     // Generate a random number  
  { 
    cout<<MIN + rand() % (MAX + MIN - 1)<<"/t";  
  } 
  cout<<endl; 
  return  0;   
}  

3. The relationship between rand() and srand()

rand() and srand() are used up to 1, where srand() is used to initialize the random number seed and rand() is used to generate random Numbers.

Since the random number seed is 1 by default, and the random number generated by the same random number seed is 1, the meaning of randomness is lost. Therefore, in order to obtain a different random number each time, the function srand() is used to initialize the random number seed. For the parameter srand(), use the time function value (the current time), since the two calls to rand() are usually made at different times, thus ensuring randomness.

4. General formula for generating 1 random number in a fixed range

To get a random integer for [a,b], use (rand() % (b-a))+ a (the result value includes a but not b).

To get a random integer for [a,b], use (rand() % (b-a +1))+ a (the result values include a and b).

To get a random integer for (a,b], use (rand() % (b-a))+ a + 1 (the result value does not include a with b).

(in general, 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 one says: a + (int)b * rand()/(RAND_MAX + 1).

To get floating point Numbers between 0 and 1, you can use rand()/double(RAND_MAX).

5. The reason for the same random number

The random Numbers of the computer are all generated by pseudorandom Numbers, that is, by the small M polynomial sequence, where each small sequence is generated with an initial value, that is, the random seed. (note: the period of the small M polynomial sequence is 65535, that is, the period of the random number generated by using a random seed is 65535. When you get 65535 random Numbers, they appear again and again.)

We know rand () function can be used to generate a random number, but it's not really in the sense of random Numbers, is a pseudo-random number, is according to the number of 1 (we can call it seeds) estimates as a benchmark for a recurrence formula of 1 series number, when the large number of series, conform to the normal release, which is equivalent to generate a random number, but this is not the real random number, when the normal boot up the computer, the seed value is settled, unless you destroy the system.

Examples are as follows:


#include <iostream> 
using namespace std; 
#include <stdlib.h> 
#include <time.h> 
 
int main() 
{ 
  int i; 
  for (i=0; i<10; i++)   // produce 10 A random number  
  { 
    cout<<rand()<<"/t"; 
  } 
  cout<<endl; 
  return 0; 
} 

The same random sequence is obtained every time:

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()1 must be placed outside the loop or outside the loop call, otherwise you get the same random number).

Example program:


#include <iostream> 
using namespace std; 
#include <stdlib.h> 
#include <time.h> 
 
int main() 
{ 
  int i; 
  srand((unsigned)time(NULL)); // Initializes a random number seed  
  for (i=0; i<10; i++)     // produce 10 A random number  
  { 
    cout<<rand()<<"/t"; 
  } 
  cout<<endl; 
  return 0; 
} 

Each run results in a different random sequence:

1294 18562 14141 18165 11910 29784 11070 13225 131 24405

1774 25714 18734 16528 20825 17189 9848 8899 2503 5375


Related articles: