Rand ()
Used to generate random numbers. Strictly speaking, the pseudo-random number is generated ). In a program, two calls to Rand () are used to generate random numbers and the results are the same.
For (INT I = 0; I <size; I ++)
{
Int x = rand () % 20;
Cout <x <Endl;
}
Used to obtain a random number of less than 20. If you use it multiple times, you can only get the same random array.
If you want to obtain two different random numbers, you can obtain them at different system times:
Srand (time (0 ));
The header file "time. H" should be included. Note that the interval between two calls should be more than one second to obtain different random arrays.
Srand (time (0 ));
For (INT I = 0; I <size; I ++)
{
Int x = rand () % 20;
Cout <x <Endl;
}
If the interval between two calls is less than 10 seconds, you can use sleep. In Windows, sleep is measured in milliseconds. in Linux, sleep is measured in seconds. In standard C, S is not capitalized. In VC, s must be capitalized. (Pay attention to it here)
Here we only talk about how to use rand () random time. We need to check the relevant information about the theory.