default random object?
Benji Smith
dlanguage at benjismith.net
Sun Feb 15 14:40:33 PST 2009
Andrei Alexandrescu wrote:
> Steve Schveighoffer wrote:
>>> 4. While we're at it, should uniform(a, b) generate by default something
>>> in [a, b] or [a, b)?
>>
>> [a,b)
>>
>> Every other piece of range-like code is zero based, and excludes the
>> upper bound. This should be no different. It makes the code simpler
>> too.
>
> I tried both versions, and it turns out my code is almost never simpler
> with open integral intervals. Most of the time I need something like:
>
> auto x = uniform(rng, -100, 100);
> auto y = uniform(rng, 0, 100);
>
> and I need to remember to actually ask for 101 instead of 100. True,
> when you want a random index in an array, open intervals are more
> convenient.
>
> One purity-based argument is that in a random number you may actually
> ask for the total range:
>
> auto big = uniform(rng, uint.max / 2, uint.max);
>
> If the interval is open I can't generate uint.max.
>
> Anyway, I checked the C++ API and it turns out they use closed intervals
> for integers and open intervals for reals. I know there's been a lot of
> expert scrutiny there, so I suppose I better copy their design.
>
>
> Andrei
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
More information about the Digitalmars-d
mailing list