Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!usc!orion.oac.uci.edu!uci-ics!bvickers From: bvickers@ics.uci.edu (Brett Joseph Vickers) Newsgroups: comp.lang.c Subject: Re: random number functions Message-ID: <256B4055.5180@paris.ics.uci.edu> Date: 23 Nov 89 00:56:52 GMT References: <887@telesci.UUCP> Reply-To: Brett Joseph Vickers Organization: University of California, Irvine - Dept of ICS Lines: 32 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