C language and C++ how to generate random Numbers

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

This article shares the C language /C++ how to generate random Numbers of the specific implementation method, for your reference, the specific content is as follows

C /C++ how to generate random Numbers: rand(), srand(), C /C++, random(int number) are not included in C /C++.
(1) if you only need to generate random Numbers without setting the range, you can just use rand() : rand() will return 1 random value between 0 and RAND_MAX. RAND_MAX is defined in stdlib.h with a value of 2147483647.
Such as:


#include<stdio.h>
#include<stdlib.h>
void main()
{
    for(int i=0;i<10;i+)
       printf("%d\n",rand());
}

(2) if you want to randomly generate a number in the range of 1, you can define an random(int number) function in the macro definition, and then directly call the random() function in main() :
For example, randomly generate 10 Numbers from 0 to 100:


#include<stdio.h>
#include<stdlib.h>
#define random(x) (rand()%x)
void main()
{
   for(int x=0;x<10;x++)
      printf("%d\n",random(100));
}
 

(3) however, the random Numbers generated by the above two examples can only be 1 time, if you run the second time and the output result is still the same as the first time. This is related to the srand() function. srand() is used to set the seed of a random number when rand() generates a random number. Before calling the rand() function to generate a random number, srand() must be used to set the random number seed (seed). If no random number seed is set, rand() will automatically set the random number seed to 1 when it is called. In the two examples above, because the random number seed is not set, the random number seed is automatically set to the same value 1 each time, resulting in the random number generated by rand() is 1.
srand() function definition: void srand (unsigned int seed);
You can usually use the return value of geypid() or time(0) as seed
If you use time(0), add the header #include < time.h >
Such as:


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)
void main()
{
   srand((int)time(0));
   for(int x=0;x<10;x++)
      printf("%d\n",random(100));
}

There are so many random Numbers in practice, such as game design and signal processing, that it is usually easy to get an average distribution of random Numbers. But how do you generate other random Numbers based on the average distribution of random Numbers? In this paper, a method based on geometric intuitive area is proposed, and the method of generating random Numbers with arbitrary distribution is discussed by taking the generation of normally distributed random Numbers as an example.

1. Generation of random Numbers with average distribution
      as we all know, random Numbers play a big role in various aspects. In the context of vc, we are provided with the library function rand() to generate a random integer. The random number is distributed on average between 0 and RAND_MAX. RAND_MAX is a constant, which is defined as follows in the environment of VC6.0:
#define RAND_MAX 0x7fff
    is the maximum value of 1 short data. If you want to generate 1 floating point random number, you can use rand()/1000.0 to get 1 random floating point number with an average distribution between 0 and 32.767. If you want to increase the range by 1 point, you can achieve an evenly distributed random number in any range by generating a linear combination of several random Numbers. For example, to generate random Numbers with an average distribution between -1000 and 1000 with an accuracy of 4 decimal places can be done like this. You start with a random integer between 0 and 10000. The method is as follows:
int a = rand()000;
Then, four decimal places are reserved to produce random decimal Numbers between 0 and 1:
double b = (double)a/10000.0;
Then, the generation of random Numbers in any range can be realized through linear combination. To achieve the average distributed random Numbers within -1000~1000, the following can be done:
double dValue = (rand()000)/10000.0*1000-(rand()000)/10000.0*1000;
So dValue is what you want.
By now, you might think you're done with the 1 cut, but you're not. If you look closely at the 1, you'll notice something is wrong.
double dValue = (rand()000)/10.0-(rand()000)/10.0;
   , the range of random Numbers produced is correct, but the accuracy is not correct, and it becomes a random number with only 1 correct decimal place, and the following 3 decimal places are all zero, which is obviously not what we want, why and what should we do?
    first looks for the reason, rand() generates random number resolution of 32767, two are 65534, and the resolution will be reduced to 10000, two are 20000 and the required resolution is 1000*10000*2=20000000, obviously far from enough. The following methods are provided to achieve the correct results:


double a = (rand()000) * (rand()00)/10000.0;
double b = (rand()000) * (rand()00)/10000.0;
double dValue = a-b;

 , then dValue is the desired result. In the following function, it is possible to generate a random number with an average distribution within a range with an accuracy of 4 decimal places.


double AverageRandom(double min,double max)
{
int minInteger = (int)(min*10000);
int maxInteger = (int)(max*10000);
int randInteger = rand()*rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger/10000.0;
}

    but there is a problem worthy of attention, the generation of random Numbers need to have a random seed, because using computer generated random number is derived by using the method of recursive, must have an initial value, known as the seeds with the machine, if not initialized random seed, so the computer has a provincial random seed truly, so every time the results of the recursive is exactly the same, so need to initialized each time the program runs on a random seed, The method in vc is to call the function srand (int), whose parameter is the random seed. However, if a constant is given, the random sequence will be exactly the same. Therefore, the time of the system can be used as the random seed, because the system time can guarantee its randomness.
    call methods is srand (GetTickCount ()), but not in every call rand () when they use srand (GetTickCount () to initialize, because now the computer running time is faster, when continuous call rand (), the system of time has not updated, so get a random seed for a period of time are the same, so only in one large number of random Numbers like 1 before a 1 times of random initialization of the seed. The following code generates 400 random Numbers with an average distribution between -1 and 1.


double dValue[400];
srand(GetTickCount());
for(int i= 0;i < 400; i++)
{
double dValue[i] = AverageRandom(-1,1);
}

The above is the entire content of this article, I hope to help you with your study.


Related articles: