Questions about random numbers in C Language
It is important to use the rand function, srand function, and macro RAND_MAX (32767) to generate random numbers using C language. They are all declared in stdlib. h.
Int rand (void); // generates a random number.
Voidsrand (unsigned int seed); // set the "seed" value for rand.
Srand () provides seed for rand (). If srand inputs the same value each time, the random number generated each time is run. The common practice is to use the following code:
Srand (unsigned) time (NULL ));
This will make the seed a non-fixed number, so that the random number will not be the same for each execution. Let's take a look at an example:
# Include
# Include
# Include
Int test_rand ()
{
Int I;
/* Seed therandom-number generator with current time so that
* Thenumbers will be different every time we run.
*/
Srand (unsigned) time (NULL ));
/* Display10 numbers .*/
For (I = 0; I <10; I ++)
Printf ("% 6d \ n", rand ());
Return 0;
}
The reason why C's function library does not directly put the important operation of initializing random seeds with the system clock into the implementation of the rand function is as follows:
1. Continuous Random numbers can be generated efficiently without every initialization;
2. Give programmers a higher degree of flexibility, because it is possible to use better data as seeds, rather than system clock, in scenarios with higher requirements;
3. Initialization is not required to generate a large number of pseudo-random numbers for some verification or statistics. The same series of random numbers are generated every time the program runs. In some cases, this does not matter.
4. As the rand () function of the pseudo-random sequence generator, the generated sequence must be reproducible. This is not just an algorithm. To a large extent, it is related to the accuracy of code testing. If the algorithm uses data related to the results of rand () and a controllable reproducible sequence, we have the opportunity to reproduce each test process, to locate the problem more effectively. So here is a suggestion. in the Code, if the result of the rand () function is related to the result of the algorithm, you must ensure that your rand () call is reproducible.
In addition, rand also has several problems:
How to generate a random number between 0 and 100?
Use "int x = rand () % 100;" this method is not or is used, so that the random number is not random. It is better to generate a random number between 0 and n:
J = (int) (n * rand ()/(RAND_MAX + 1.0 ));
How to generate a random number in the range of (a, B?
Calculate the difference between a and B and set c = B-a; generate a value between 0 and B-.
D = (int) (B-a) * rand ()/(RAND_MAX + 1.0)
Add a to the value d generated above.
If you use C ++ 11 for programming, use the random number generation method of C ++ 11!
Although I have introduced a lot above, I still want to say that the random number generation method in C language has many defects, so it is easy to introduce non-randomness and has a single function. If possible, you 'd better avoid using it.