Mail Archives: djgpp/1997/10/07/05:17:24
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 -