The use of the random function random in C++

  • 2020-05-30 20:46:11
  • OfStack

The use of the random function random in C++

1. random function is not ANSI C standard and cannot be compiled under gcc,vc and other compilers. It can be implemented by using the rand function under C++.

1. C++ standard library provides 1 random number generator rand, which returns pseudo-random integers evenly distributed between 0 and RAND_MAX. RAND_MAX must be at least 32767. The rand() function does not accept arguments and is seeded with 1 by default (that is, the starting value). Random number generators always start with the same seed, so the resulting pseudo-random sequence is the same and loses its random meaning. (but it's easy to debug)

2. Another function in C++, srand (), can specify different Numbers (unsigned integer arguments) as seeds. But if the seeds are the same, the pseudo random sequence is the same. One option is to get the user to enter a seed, but it's still not ideal.

3. It is ideal to seed a random number generator with variable Numbers, such as time. The value of time varies from moment to moment. So the seed is different, so the random Numbers are different.


// C++ Random function ( VC program )  
#include <stdlib.h> 
#include <iostream.h> 
#include <time.h> 
#define MAX 100 
void main()
{ 
    srand( (unsigned)time( NULL ) );//srand() Function to produce 1 A random seed that starts at the current time  
   for (int i=0;i<10;i++) 
   cout<<rand()%MAX<<endl;//MAX Is the maximum value, and its random field is 0~MAX-1
} 

2. Use of rand()

rand() takes no arguments and returns an arbitrary integer from 0 to the largest random number, which is usually a fixed large integer. In this way, if you want to produce 10 integers from 0 to 10, you can express it as:


  int N = rand() % 11; 

In this way, the value of N is a random number from 0 to 10. If 1 to 10 is generated, it is like this:


  int N = 1 + rand() % 10; 

In summary, it can be expressed as:


 a + rand() % n 

Where a is the starting value and n is the range of integers. If you want a decimal from 0 to 1, you can get an integer from 0 to 10, and then divide by 10 to get 10 random decimal places from random to 10. If you want a random decimal place from random to 100, you need to get 10 integers from 0 to 100, and then divide by 100
So on.

Generally, rand() generates the same random number each time it runs as it did the previous time, which is intentionally designed to facilitate debugging of the program. To generate a different random number each time, you can use the srand(seed) function to randomize, and different random Numbers can be generated with the difference of seed.

As you said, you can also include the time.h header file and then use srand(time(0)) to randomize the random number generator with the current time so that you can ensure that you get a different sequence of random Numbers every two runs (as long as the interval between the two runs is more than 1 second).

Note: rand() generates a random number on 0 to RAND_MAX (32767), which is not divisible by 11.

So int N = rand() % 11; The random number you get is not a distribution from 0 to 101 and the probability of 9,10 and 0 to 8 is 11/32767 less than 1/2978;

If M is larger, for example, M=30000, then the probability of 0-2767 is twice as high as that of the following Numbers, which is seriously inconsistent with the random distribution!

3. Set the probability as required

For example, to set a 10% probability problem, we can take the rand () function to realize it. In the judgment of if conditional sentence, we can use the value %1 obtained by rand () to set one value, and then do "==" operation with another value.

Such as:


if ( 1==rand (a) %10 ) 
{ //10% The probability of achieving here is edited for the rest of the code }
else
{ //90% So I'm going to edit the rest of the code }

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: