🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.amiga.tech
Article #7204

Re: rand() within a range.

#7204
From: Dolf Grunbauer
Date: Wed, 05 Dec 1990 07:43
47 lines
1584 bytes
In article <666@sheoak.bcae.oz> 737104@sheoak.bcae.oz (David Thiele) writes:
>Is there a way to generate random numbers within a certain range in 
>Lattice C??. Ive done it with the following code but it's not very
>elegant (or fast!),
>
>Random(range,seed)
>int range,seed;
>{
>	srand(seed);
>	if range = 0
>		return(0);
>	else{
>		do
>			seed = rand() / 10000000;
>		while (seed >= range);
>		return(seed);
>	}
>}
>

How about:
Random(range,seed)
int range,seed;
{
	srand(seed);
	if range = 0
		return(0);
	else	return (((seed = rand()) / 10000000) % range);
}

By the way: why do you want to set seed explicitly before each rand call ?
Propably what will do also is:

Random(range)
int range;
{
	if range = 0
		return(0);
	else	return (rand() % range);
}

and call 'srand' only in some initialisation function or main.
-- 
   _ _ 
  / U |  Dolf Grunbauer  Tel: +31 55 433233 Internet dolf@idca.tds.philips.nl
 /__'<   Philips Information Systems        UUCP     ...!mcsun!philapd!dolf
88  |_\  If you are granted one wish do you know what to wish for right now ?
Technical Details
Message-ID: <540@ssp9.idca.tds.philips.nl>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!mcsun!hp4nl!philapd!ssp9!dolf
References: <666@sheoak.bcae.oz>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!mcsun!hp4nl!philapd!ssp9!dolf
From: dolf@idca.tds.PHILIPS.nl (Dolf Grunbauer)
Newsgroups: comp.sys.amiga.tech
Subject: Re: rand() within a range.
Message-ID: <540@ssp9.idca.tds.philips.nl>
Date: 5 Dec 90 07:43:18 GMT
References: <666@sheoak.bcae.oz>
Reply-To: dolf@idca.tds.philips.nl (Dolf Grunbauer)
Organization: Philips Information Systems, Apeldoorn, The Netherlands
Lines: 47