default random object?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Feb 13 21:26:25 PST 2009


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



More information about the Digitalmars-d mailing list