random number within a specified range?
jarrett.billingsley at gmail.com
Sun Oct 5 16:37:36 PDT 2008
On Sun, Oct 5, 2008 at 7:34 PM, Michael P. <baseball.mjp at gmail.com> wrote:
> How would I go about getting a random number that was in a specified range of 2 numbers?
> For example, getting a number inbetween 1000 and 2000.
> -Michael P.
lo + rand() % (hi - lo)
assuming you're using Phobos, where rand() returns a number in the
range [0, uint.max].
This will get you a number in the range [1000, 2000); that is, it will
be at least 1000 and at most 1999. If you need 2000 to be one of the
numbers generated, use 2001 as the hi.
More information about the Digitalmars-d-learn