default random object?

Benji Smith dlanguage at benjismith.net
Sun Feb 15 15:17:24 PST 2009


Benji Smith wrote:
> Maybe a NumericInterval struct would be a good idea. It could be 
> specialized to any numeric type (float, double, int, etc), it would know 
> its own boundaries, and it'd keep track of whether those boundaries were 
> open or closed.
> 
> The random functions would take an RND and an interval (with some 
> reasonable default intervals for common tasks like choosing elements 
> from arrays and random-access ranges).
> 
> I have a Java implementation around here somewhere that I could port to 
> D if anyone is interested.
> 
> --benji

Incidentally, the NumericInterval has lots of other interesting 
applications. For example

    auto i = NumericInterval.UBYTE.intersect(NumericInterval.SBYTE);
    bool safelyPolysemious = i.contains(someByteValue);

    auto array = new double[123];
    auto i = NumericInterval.indexInterval(array);
    bool indexIsLegal = i.contains(someIndex);

Using a numeric interval for generating random numbers would be, in my 
opinion, totally ideal.

    double d = uniform(NumericInterval.DOUBLE); // Any double value

    auto i = NumericInterval.parse("[ 123, 456.789 )");
    double random = uniform!(double)(i, rng);

--benji



More information about the Digitalmars-d mailing list