/****************************************************************
** File: rand.c
** Author: Marie desJardins
** Date: 9/6/06
** Section: 0101
** E-mail: mariedj@cs.umbc.edu
**
** This file shows you how to generate a random number
** between 1 and 5.
****************************************************************/
#include
#include
int main () {
unsigned seed;
/* This "seeds" the random number generator. If you omit this
step, you will always get the same sequence of random numbers
(which is useful for testing). If you enter the same seed, you
will also get the same sequence of random numbers. */
printf ("Enter random seed: ");
scanf ("%u", &seed);
srand(seed);
/* rand() returns a random integer between 0 and RAND_MAX (a
constant defined in stdlib.h). To get a random integer between
1 and N, you must first take the modulus of the random number
with N (which will give you a random integer between 0 and N-1),
then add 1. */
printf ("Here's a random number between 1 and 5: %d\n", rand()%5 +1);
return(1);
}