Random numbers

0 Marcus Sinclair · October 29, 2014
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
   int i;
   time_t t;



   /* Intializes random number generator */
   srand((unsigned) time(&t));

   /* Print 5 random numbers from 0 to 50 */
   for( i = 0 ; i < 5 ; i++ ) {
      printf("%d\n", rand() % 10)+1;
   }

  return(0);
}

Hi,

I found the above code to use for generating random numbers. Can anyone explain how the srand line & time_t t line works?

Post a Reply

Replies

Oldest  Newest  Rating
0 Marcus Sinclair · October 31, 2014
Thanks for the reply. That's a little clearer now.
  • 1

C

106,964 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator