Date: Tue, 7 Oct 1997 13:52:36 -0400 (EDT) From: "Art S. Kagel" Reply-To: kagel AT ns1 DOT bloomberg DOT com 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)) > x=rand(); ^^^^ You test the value of x then get a random (maybe huge) value and print ^^^^ it. Try this: do { x=rand(); /* Get a random value */ } while((x < 0)||(x > MaxValue)); /* Loop until value is in range. */ > printf("[%d]\n",x); > } > > void main(void) > { > int i; > printf("Press return to get 10 numbers between 0-100\n"); > getch(); > for(i=0;i<10;i++) > Random(100); > printf("\n"); > getch(); > } > ------------------------------------------------------------------ > > Why does it keep giving me HUGE numbers? > Please help this BLIND man see!!! Art S. Kagel, kagel AT bloomberg DOT com