random number functions
Brett Joseph Vickers
bvickers at ics.uci.edu
Thu Nov 23 11:56:52 AEST 1989
In article <887 at telesci.UUCP> jpoplaws at telesci.UUCP (Joseph E Poplawski) writes:
>Hello. I am wondering if anyone has any functions that they could share with
>me for random numbers. What I need is two functions. One to generate a
>number between 0 and z, and another to generate a number between x and z.
>I need it to generate good random numbers. I have been trying to do such
>for two days now and I have yet to come up with anything successfully.
Try this one:
int mrand(begin,end)
int begin, end;
{
int r;
r = rand() % (end-begin+1) + begin;
return(r);
}
This function will work for your x-z range as well as your 0-z range. Also,
don't forget to seed the random number generator (only ONCE!) using a time-
related function. I usually use the following call:
srand(time()%1000);
This was written on a XENIX system. UNIX may be slightly different. I
believe you might have to use irand() rather than rand() to get the necessary
random value.
--
bvickers at bonnie.ics.uci.edu
More information about the Comp.lang.c
mailing list