How to Generate a Sequence of Random Numbers

Standard C provides two functions for the purpose of random number generation–rand and srand. The function rand generates a sequence of pseudo-random numbers. The term pseudo-random implies that the generated sequence is not really random: no matter how many times you execute the program, the same sequence of numbers will be generated. To create a truly unique sequence of numbers every time the program executes, you should use the function srand:

 #include  //rand and srand declarations#include  //printf declaration#include  //time declarationusing namespace std;void main() { srand ( time(NULL) ); //set starting-point for a random sequence of numbers  for (int j =0; j < 10; j++)   printf ("%d
", rand() );}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: