Objective
In the process of programming the algorithm, we often need to use random numbers. Since the computer is a logically based machine, it is impossible to do a real random (probably quantum computer?). )。 So the computer generates pseudo random numbers for us to use.
We use the RAND function of the C language, which is also a pseudo random number.
The use of the RAND function in C language
1. Write header file
#include <stdlib.h> #include <stdio.h> #include <time.h>
2, the definition of the variable
void Main (void) {int i,k;
3, Srand ((unsigned) time (NULL)); /* Select Seed File/* *
4, for (i = 0; I <20;i++)/* Loop control 20 random number of generation/{K=rand ()%100;/* Store random number/printf ("k=%d\n", k); * Output random number *}}
1, this is a random function of a generation method
2, if only one, then can omit circular control
A simple demonstration is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
main (int argc, char* * argv)
{
//To construct the "seed" of the pseudo random number in the current time of the machine.
Srand ((unsigned int) time (NULL));
int i;
Print 10 pseudo random numbers
for (i = 0; i < i++) {
printf ("%d", Rand ());
}
printf ("\ n");
System ("pause");
return 0;
}
Summarize
The above is about the use of the RAND function in C language all the content, I hope this article on the daily use of C language can help.