C - Seed the random numbers using current time

Introduction

In the following code, the seed value is returned from the system clock by using the time() function.

Demo

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

int main() //  w ww . j  av a 2 s. co  m
{ 
   int r,a,b; 

   srand((unsigned)time(NULL)); 
   for(a=0;a<20;a++) 
   { 
       for(b=0;b<5;b++) 
       { 
           r=rand(); 
           printf("%d\t",r); 
       } 
       putchar('\n'); 
   } 
   return(0); 
}

Result

The time() function returns information about the current time of day.

Related Topic