<1>
How to generate random numbers in C/C ++: the rand () function, srand () function, and C Language/C ++ do not have the built-in random (INT number) function.
(1) If you only need to generate a random number without setting a range, you only need to use rand (): rand () returns a random value, the value range is 0 to rand_max. Rand_max is defined in stdlib. h and its value is 2147483647.
For example:
# Include
# Include
Void main ()
{
For (INT I = 0; I <10; I +)
Printf ("% d/N", Rand ());
}
(2) If you want to generate a random number in a certain range, you can define a random (INT number) function in the macro definition, and then directly call random () in main () function:
For example, a random generation of 10 0 ~ Number of 100:
# Include
# Include
# 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 in the preceding two examples can only be one-time. If the output result is the same as that of the first running. This is related to the srand () function. Srand () is used to set the random number seed when rand () generates a random number. Before calling the rand () function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set () during the call, the random seed is set to 1. In the above two examples, because no random number seed is set, each random number seed is automatically set to the same value of 1, which leads to the same random value produced by rand.
Srand () function definition: void srand (unsigned int seed );
Generally, the return value of geypid () or time (0) can be used as seed.
If you use time (0), add the header file # include
For example:
# Include
# Include
# Include
# Define random (x) (RAND () % x)
Void main ()
{
Srand (INT) time (0 ));
For (INT x = 0; x <10; X ++)
Printf ("% d/N", random (100 ));
}
In this way, the two running results will be different !!
<2>
The function rand () in the Standard C library can generate 0 ~ A random number between rand_max. rand_max is an integer defined in stdlib. H, which is related to the system.
The rand () function is referenced directly by the expression rand () without any input parameter. For example, you can use the following statement to print two random numbers:
Printf ("random numbers are: % I/N", Rand (), Rand ());
Because the rand () function generates integers in the specified order, the two identical values are printed each time the preceding statement is executed, therefore, the C language is not random in the positive sense.
To generate a random value of a new sequence during each execution, we usually provide a new random seed for the random number generator. The srand () function (from stdlib. h) can spread seeds for the random number generator. As long as the seeds are different, the Rand () function will generate different random number sequences. Srand () is called the initialization tool of the random number generator.
Routine:
File Name:
Rand_srand.c
# Include
# Includ
Int main ()
{
Usigned int seed;
Int K;
Pringt ("enter a positive integer seed value:/N ");
Scanf ("% u", & seed );
Srand (SEED );
Printf ("random numbers are:/N ");
For (k = 1; k <= 10; k ++)
Printf ("% I", Rand ());
Printf ("/N ");
Return 0;
}
You will find that when the seeds you provide are the same, the random number sequence is also the same. In addition, when the seed is 1, it is the same as when the srand () function is not used, that is, the initial seed value of the Rand () function is 1 by default;
In stdlib. H, the two functions are prototype:
Int rand ();
Void srand (unsigned INT );
Expansion:
X = rand ();
Y = rand () q-25;
Z = (double) rand ()/rand_max) * (B-A) +;
<3>
1-0: how Microsoft Vc ++ generates random numbers:
Srand () and rand () functions. Essentially, y = ax + B (mod m) is used in the linear same remainder method ). Where a, B, and m are constants. Therefore, the generation of Rand is determined by X, which is called seed. Seed needs to be set in the program. Generally, the system time is used as the seed. The correlation between random numbers is very small. The value range is 0-32767 (INT), that is, double bytes (16 digits). If the unsigned int double byte is 65535, the four-byte value is 4294967295, which generally meets the requirements.
1-1: Linear coremainder method:
? /P>
M is the modulus, A is the multiplier, and C is the increment, which is the initial value. When C = 0, this algorithm is called the multiplication same method. If C = 0, the algorithm is called the hybrid Coordinator method. When C is taken from an appropriate value that is not zero, it has some advantages, but the advantages are not prominent. Therefore, c = 0 is usually used. M is the main sign of the cycle length of the generator, M is a prime number, take a m as the original root, the cycle T = M-1. For example:
A = 1220703125
A = 32719 (This number is used in the Program)
A = 16807
Code:
Void main ()
{
Const int n= 100;
Double A = 32719, M = 1, F [n + 1], G [N], seed;
M = POW (2,31 );
Cout <"set m value to" <M-1 <Endl;
Cout <"input seed" <Endl; // enter Seed
Cin> seed;
F [0] = seed;
For (INT I = 1; I <= N; I ++) // generate a random number using linear Coordinator
{
F [I] = fmod (A * f [I-1]) m-1 ));
G [I-1] = f [I]/(S-1 );
Cout. SETF (IOs: fixed); cout. Precision (6); // you can specify the output precision.
Cout <I <"<'/t' <G [I-1] <Endl;
}
}
Result Analysis: the average value of the statistical data is 0.485653.
Statistical data variance: 0.320576
1-2: Human-character ing
Recurrence Formula
? /P>
It is the "man-character ing" or "tent ing" in the famous chaotic ing. Its distribution density function of non-cyclic orbital points: the combination of man-character ing and linear Coordinator method, A uniform random number with excellent statistical properties can be generated.
For (INT I = 1; I <= N; I ++) // generate a random number using linear Coordinator
{
F [I] = fmod (A * f [I-1]), M );
If (F [I] <= m/2) // a random number is generated based on the human-character ing.
{
F [I] = 2 * f [I];
}
Else
{
F [I] = 2 * (m-f [I]) + 1;
}
1-3: China and France -- Feng nuoman
Before and after 1946, Feng nuoman proposed that his solution was to extract the square of the random number and the numbers in the middle. For example, a 10-digit number is generated, and the previous value is 5772156649, and the square result is 33317792380594909201. Therefore, the next number is 7923805949.
For (j = 1; j <= N; j ++)
{
I [J] = I [J-1] * I [J-1];
I [J] = I [J]/POW (10, 5 );
I [J] = fmod (I [J], POW (10, 10 ));
G [J] = I [J]/POW (10, 10 );
Cout. SETF (IOs: fixed); cout. Precision (6); // you can specify the output precision.
Cout <j <'/t' <G [J] <Endl;
}
Ii. Arbitrary distribution of random numbers
Random numbers with a uniform distribution of () can be used to generate random numbers with arbitrary distribution. The main methods include inverse function method, selection method, discrete approximation method, limit approximation method, and random variable function method. The inverse function method is discussed here. Of course, different methods can be used for specific distribution functions.
If the random variable X has the distribution function f (x), the value of X is
Inv indicates the inverse function. Assume that R is a value of the random variable R with a uniform distribution of (). The distribution function of the known R is
Therefore, if R is a value of R, X has a probability.
That is to say, if (R1, R2,..., RN) is a group of values of R, a group of values can be obtained accordingly.
With distribution. Therefore, if we know the inverse function of the distribution function, we can obtain the random number from the random number of the (0, 1) distribution.
1-4: exponential distribution:
The exponential distribution function is:
X <0, f (x) = 0;, f (x) = 1-exp
Using the inverse function method described above, we can obtain: x = ln (1-y). Here we may take the constant as 1.
For (Int J = 0; j
{
I = rand () 0; // generates any value from 0-32767
A [J] = double (I)/double (100 );
A [J] =-log (A [J]); // The constant is greater than 0. Here, 1 is used.
,,,,,,,
1-5: Normal Distribution:
The probability density of a normal distribution is:
The distribution function of the normal distribution is:
For normal distribution, it is obviously difficult to use the inverse function method to obtain the normal distribution sequence, which involves complicated Integral Differential Operations. For convenience, we can obtain the standard normal distribution. Two algorithms are introduced here:
First:
Box and Muller provided an algorithm for generating a normally distributed random variable from a uniformly distributed random variable in 1958. Where U1 and U2 are random variables with a uniform distribution on the intervals (0, 1), they are independent of each other. Ling
X1 = SQRT (-2 * log (U1) * Cos (2 * pI * U2 );
X2 = SQRT (-2 * log (U1) * sin (2 * pI * U2 );
Therefore, X1 and X2 are subject to the N () distribution and are independent of each other.
P = rand () 0; // generates any value from 0-32767
B [J] = double (P)/double (0, 100 );
A [J] = SQRT (-2 * log (A [J]) * Cos (2x3.1415926 * B [J]);
Second:
Generate a standard normal distribution. Multiple Random Variables and their distributions in the same distribution are close to the normal distribution. K random variables (0, 1) with uniform distribution are obtained ,,...... , The sum of them follows the normal distribution.
In practice, take K = 12 (because D () = 1/12), then the new random variable Y = X1 + X2 +... + x12-6, we can find the expected mathematical E (y) = 0, variance d (y) = 12*1/12 = 1, so we can approximate the standard normal distribution.