This article shares the C language/c++ how to generate a random number of specific implementation methods for your reference, the specific contents are as follows
C language/c++ How to generate random numbers: Here is the rand () function, the Srand () function, the C language/c++ does not have its own random (int number) function.
(1) If you want to generate random numbers without setting a range, you can just use RAND (): rand () returns a random value ranging from 0 to Rand_max. Rand_max is defined in Stdlib.h, and its value is 2147483647.
For example:
#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 within a certain range, you can define a random (int number) function in the macro definition, and then call the random () function directly in main ():
For example: randomly generated number of 10 0~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);
}
(3) But the random numbers generated by the above two examples can only be one-time, if you run the second time the output is still the same as the first time. This is related to the Srand () function. Srand () is used to set the random number seed for rand () to produce random numbers. Before you can invoke the rand () function to generate random numbers, you must first use Srand () to set up the random number seed (seed), and if no random number seed is set, rand () automatically sets the random number seed to 1 when it is called. The above two examples are because no random number seed is set, and each random number seed is automatically set to the same value of 1, which results in the same random values produced by Rand ().
Srand () function definition: void srand (unsigned int seed);
You can usually use the return value of Geypid () or time (0) as a seed
If you use Time (0), add the header file #include<time.h>
For example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RANDOM (*) (rand ()%x)
void Main ()
{
srand ((int) time (0));
for (int x=0;x<10;x++)
printf ("%d\n", Random);
}
Random number in the actual use of very much, such as game design, signal processing, usually we can easily get the average distribution of random numbers. But how do we generate random numbers of other distributions based on the average distribution of random numbers? In this paper, a method based on the geometrical visual area is presented, and the generation of random numbers is discussed with the example of normal distribution.
generation of the average distributed random number
As we all know, random numbers in all aspects have a great role in the VC environment, for us to provide the library function rand () to produce a random integer. The random number is averaged evenly between 0~rand_max, and Rand_max is a constant, defined in the VC6.0 environment:
#define RAND_MAX 0X7FFF
It is the maximum value of a short data, and if you want to produce a random number of floating-point types, you can get rand ()/1000.0 to obtain a random floating point that is evenly distributed between 0~32.767. If you want to make the range larger, you can achieve the average distribution of random numbers in any range by generating a linear combination of several random numbers. For example, a random number that produces an average distribution of four-bit precision between -1000~1000 can be implemented in this way. First, a random integer from 0 to 10000 is generated. The method is as follows:
int a = rand () 000;
The four-bit decimal is then retained to produce random decimals between 0~1:
Double b = (double) a/10000.0;
Then the random number in any range can be produced by linear combination, and the random number of the average distribution within the -1000~1000 can be achieved:
Double Dvalue = (rand ())/10000.0*1000-(rand () 000)/10000.0*1000;
Then the dvalue is the value you want.
So far, you may think that all the work has been done, but in fact, if you look carefully, you will find that there is a problem, and the above simplification becomes:
Double Dvalue = (rand ())/10.0-(rand () 000)/10.0;
In this way, the resulting random number range is correct, but the accuracy is not correct, has become only a correct number of decimal random numbers, the back three decimal places are zero, obviously not what we ask, what reason, and how to do it.
First look for the reason, Rand () produced a random number resolution of 32767, two is 65534, and after the resolution has to be reduced to 10000, two is 20000 and the resolution required for 1000*10000*2=20000000, obviously far from enough. The following methods provide the correct results:
Double A = (rand ()) * (rand ())/10000.0;
Double b = (rand ()) * (rand ())/10000.0;
Double dvalue = a-b;
Then the dvalue is the desired result. In the following function you can achieve a random number that produces an average distribution within an interval, with a precision 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's a noticeable problem, the generation of random numbers requires a random seed, because the random number produced by the computer is obtained by means of a recursive method, which must have an initial value, which is usually called a random seed, and if the random seed is not initialized, then the computer has a random seed of certainty, So each recursive result is exactly the same, so it is necessary to initialize random seeds every time the program is run, the method in VC is to call Srand (int), whose parameters are random seeds, but if you give a constant, the resulting random sequence is exactly the same, so you can use the system's time As a random seed, because the system time can guarantee its randomness.
The calling method is Srand (GetTickCount ()), but it cannot be initialized with Srand (GetTickCount ()) each time the rand () is invoked, because the computer is running faster, and when Rand () is continuously invoked, The time of the system has not been updated, so the random seeds obtained are identical for a period of time, so it is generally only a random seed initialization before a large number of random numbers are produced. The following code produces 400 random numbers that are evenly distributed between -1~1.
Double dvalue[400];
Srand (GetTickCount ());
for (int i= 0;i < i++)
{
double dvalue[i] = Averagerandom ( -1,1);
}
The above is the entire content of this article, I hope to help you learn.