Random number generator functions? Help?!
Joseph E Poplawski
jep at fantasci.UUCP
Tue Nov 21 11:04:47 AEST 1989
I am trying to get two random number routines to work, one I have named 'rnd1'
and the second 'rnd2'. What I would like is to have rnd1 return an integer
that ranges from 1 to the value of lim (which is passed as an argument) and
rnd2 to return an integer that ranges from lower to upper (which are also
passed as arguments). It seemed to compile okay, but upon execution they
constantly return the same numbers even though I reseeded the random function
at each call.
Any ideas? I have included the code following my signature.
By the way, this program is being written on an 80386 running ISC UNIX V.3.
-Jo
--
Joseph E Poplawski (Jo)
Home: 5 Sussex Drive, Willingboro, NJ 08046-1407 Phone: +1 609 835-2846
UUCP: ...{rutgers!rochester!moscom,princeton,pyrnj}!telesci!fantasci!jep
-----------------------------------------------------------------------------
main()
{
long int lr, rr;
int i;
printf ("rnd2 returns:\n");
for (i=0; i<15; ++i)
printf ("%ld, ", rnd2(1,20));
printf ("\n");
for (i=0; i<15; ++i)
printf ("%ld, ", rnd2(1,20));
printf ("\n");
}
rnd1(lim)
int lim;
{
long int lrand48(), time();
srand(rand());
srand48( (int) time ((long int *) 0));
return((lrand48() % lim) + 1);
}
rnd2(lower, upper)
int lower, upper;
{
int top = (upper - lower);
long int lrand48(), time();
srand(rand());
srand48( (int) time ((long int *) 0));
return((lrand48() % (top + 1)) + lower);
}
More information about the Comp.lang.c
mailing list