From: firewind Newsgroups: comp.os.msdos.djgpp Subject: Re: [Q] Whats wrong here... Date: 7 Oct 1997 05:30:34 GMT Organization: Netcom Lines: 48 Message-ID: <61chdq$23i@dfw-ixnews6.ix.netcom.com> References: <61canb$kjf$1 AT kurica DOT wt DOT com DOT au> NNTP-Posting-Host: elp-tx1-03.ix.netcom.com Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk 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)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ What is the point of this test? You just assigned '-1' to x, so it will always be less than 0. > x=rand(); > printf("[%d]\n",x); > } > void main(void) ^^^^^^^^^^^^^^^ This is invalid. It should be 'int 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!!! Because the algorithm used by rand() tends to produce large numbers, and despite the fact that you are passing a 'MaxValue' to 'Random()', you never pare down the return from rand() in comparison to it. -- [- firewind -] [- email: firewind AT metroid DOT dyn DOT ml DOT org (home), firewind AT aurdev DOT com (work) -] [- "You're just jealous because the voices talk to -me-." -] [- Have a good day, and enjoy your C. -]