Mail Archives: djgpp/1997/10/07/09:11:07
On Tue, 7 Oct 1997, Robert Debeljakovic 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))
^^
^^ 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.
- Raw text -