delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/07/05:17:24

From: firewind <firewind AT metroid DOT dyn DOT ml DOT org>
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
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Robert Debeljakovic <rdebelj AT wt DOT com DOT au> wrote:
> Ok, look at the following code....
> ----------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
> #include <conio.h>

> 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("<press any key to continue>\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.		            -]

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019