Date: Tue, 7 Oct 1997 15:06:57 +0200 (MET DST) From: Wojciech Piechowski To: Robert Debeljakovic cc: djgpp AT delorie DOT com Subject: Re: [Q] Whats wrong here... In-Reply-To: <61canb$kjf$1@kurica.wt.com.au> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Tue, 7 Oct 1997, Robert Debeljakovic wrote: > Ok, look at the following code.... > ---------------------------------------------------- > #include > #include > #include > > void Random(int MaxValue) > { > int x; > > x= -1; > if((x < 0)||(x > MaxValue)) ^^ ^^ I recommend while((x < 0)... > x=rand(); > printf("[%d]\n",x); > } > However, the algorithm you use is not effective: rand() returns an unsigned integer in a range 0..0x7fffffffL (it's over 2*10^9). It may take a while until it returns a number <100. For small MaxValue (10,100.. 1 000 000) you can write void Random(int MaxValue) { int x; x=rand() % MaxValue; printf("[%d]\n",x); } The larger MaxValue, the less uniform distribution you get.