by Dinesh Thakur Category: Function and Pointer

In many cases and particularly in computer games, a seed number cannot be provided on every event. We need some function whose return value is always changing. Time is always changing; so, we make use of function time() defined in the header file <time.h>.

The function time (Null)gives a number which is equal to the number of seconds elapsed till now since 1st January 1970 Greenwich time 00:00:00. This number is always changing. The problem of using a different number as seed number is solved by the following code:

long unsigned seed.= time(Null);

srand (seed);

Or we may use the following code:

srand (time(Null));

Program  uses srand (time (Nu11)); as well as displays the value of seed number.

#include<stdio.h>

#include<stdlib.h>
#include <time.h>
main()
{
  int n;
  long unsigned seed;
  clrscr();
  seed= time(NULL);
  srand (seed);
  printf("seed = %d\n", seed);
  printf("The random numbers are as below.\n");
  for (n=1;n<7;n++)
  printf("%d\t", rand())
  printf ("\n");
  return 0;
}

The expected output is as given below.

    C Program use of time() as seed number

Popular Article