summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/rand.c
blob: 2967c8a08188dc5b947235e12e2420b7fc8d7b94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*********************************************************************
 *
 * Purpose: Demonstrate the 'srand' and 'rand' functions
 * Author:  M.J. Leslie.
 * Date:    11=Nov-94
 *
 *********************************************************************/

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

main()
{
  int rolls=4;
					/* This looks DISCUSTING!
					 * time returns a different value 
					 * on every execution. And so 
					 * changes the value passed to 
					 * srand. See CAST
					 * for an explanation of 
					 * (unsigned int) and (time_t)	*/
  srand((unsigned int)time((time_t *)NULL));

					/* Roll the dice.		*/
  while(rolls--)
  {
    printf("Dice value is %d\n", (rand()%6)+1);
  }
}