Mail Archives: djgpp/1997/09/11/05:02:35
J.E. wrote:
>
> I was just wondering: How do you create a random number in Djgpp? I
> tried setting a value for RAND_MAX and then going c = rand(), but that
> didn't work (it said that the value for RAND_MAX, which was 2, was
> invalid. Any help would be appreciated.
>
> Jordan Ellis <cellis AT voyageur DOT ca>
RAND_MAX is already defined in "stdlib.h" and you may not change it
(well, you may, but this is considered bad practice, and it probably
won't change the range of numbers 'rand()' will produce) RAND_MAX
is there to let you know what to expect, and not to control 'rand()' ..
one simple way to obtain (integer) random number between 0 and arbitrary
number M is to use the remainder operator (%) for instance
b = rand()%(M+1) ;
will assign a random number between 0 and M into 'b'.
note that M needs to be considerebly smaller than RAND_MAX in order
for 'b' to be 'reasonably' evenly distributed
i.e: prob(b==0) = prob(b==1) = ... = prob(b==M) = 1/(M+1)
-amit
(there should only be single 'f' in my addrress)
- Raw text -