Mail Archives: djgpp/1997/11/04/21:15:40
Mark Phillips wrote:
] Just for the heck of it, say I want to get a random number from 0 to
] 19. rand() and random() appear to return numbers from 0 to 2^31. I
] could always do rand() * 20 / 2^31 but there must be a better way.
note: 2^31 does not mean 2 raised to the 31st power; it means 2
exclusive-ORed with 31, which is 29.
Ryan Blazecka wrote:
> How about rand() % 19? :)
You meant `rand() % 20'.
Carolyn Kelly-Pajot wrote:
: if you want a number from 0-19, type
: int random_number = rand() % 20;
: This returns the REMAINDER from division by 20, which will be between 1 and 19
: inclusive.
... and you meant `between 0 and 19 inclusive'. :)
--
Regards,
george DOT foot AT merton DOT oxford DOT ac DOT uk
- Raw text -