[comp.lang.c] random number functions

jpoplaws@telesci.UUCP (Joseph E Poplawski) (11/21/89)

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.

This is needed for an 80386 machine running ISC UNIX V.3...

Thanks in advance.

-Jo
--
Joseph E Poplawski   ...!princeton!pyrnj!telesci!fantasci!jep
		     ...!rutgers!rochester!moscom!telesci!fantasci!jep

bvickers@ics.uci.edu (Brett Joseph Vickers) (11/23/89)

In article <887@telesci.UUCP> jpoplaws@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@bonnie.ics.uci.edu