default random object?
Jason House
jason.james.house at gmail.com
Sat Feb 14 05:19:23 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
D uses a..b notation as part of the language to mean "[)". Because of that, it makes more sense to make that the default in D vs. other languages that don't have such constructs.
More information about the Digitalmars-d
mailing list