C and C++ generates random number function

  • 2020-05-10 18:31:48
  • OfStack

The random Numbers of the computer are all generated by the pseudo-random Numbers, that is, by the small M polynomial sequence, where each small sequence generated has 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 is not the real random number, 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.

1.rand()
Function: random number generator  
Usage :int rand(void)  
Where header file: stdlib.h
The internal implementation of   rand() is done using the linear congruence method. It is not really a random number, but can be regarded as random in a definite range of 1 due to its extremely long period.
  rand() returns 1 random value between 0 and RAND_MAX. The range of RAND_MAX is at least between 32767 (int). Using unsigned int double bytes is 65535, and 4 bytes is the integer range of 4294967295. 0~RAND_MAX each number has the same probability of being selected.  
When the user does not set the random number seed, the default random number seed of the system is 1.  
rand() generates a pseudorandom number that is the same every time it is executed; To be different, initialize it with the function srand().

2.srand()
Function: initialize random number generator
Usage: void srand(unsigned int seed)
Location header: stdlib.h  
srand() is used to set the random number seed when rand() generates a random number. The parameter seed must be an integer. If seed is set to the same value every time, the random value generated by rand() will be one every time.

3. Use the current clock as a random number seed
rand() generates the same random number each time it is run as it did the previous time. To be different, initialize it with the function srand(). You can use the method srand((unsigned int)(time NULL)) to generate different random number seeds, because the time of each run is different.

4. Use of generating random Numbers
1) provide a seed for srand(), which is of type unsigned int;
2) call rand(), which will return a random number (between 0 and RAND_MAX) based on the seed value provided to srand();
3) call rand() multiple times as needed, so as to continuously get new random Numbers;
4) at any time, a new seed can be provided to srand() to further "randomize" the output of rand().

Random number program between 0~RAND_MAX


#include <iostream> 
#include <stdlib.h> 
#include <time.h> 

using namespace std; 

int main() 
{ 
  srand((unsigned)time(NULL)); 
  for(int i = 0; i < 10;i++ ) 
    cout << rand() << '\t'; 
  cout << endl; 
  return 0; 
}
 

5. General formula for generating 1 random number in a fixed range
To get a random integer of [a,b], use (rand() % (b-a))+ a;
To get a random integer for [a,b], use (rand() % (b-a +1))+ a;
To get a random integer for (a,b], use (rand() % (b-a))+ a + 1;
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 is: a + (int)b * rand()/(RAND_MAX + 1).
To get floating point Numbers between 0 and 1, you can use rand()/double(RAND_MAX).

The above is to introduce c/c++ random number function in detail, I hope to help you learn


Related articles: