default random object?

KennyTM~ kennytm at gmail.com
Fri Feb 13 22:45:13 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.
> 

Use uniform!("[]")(rng, uint.max/2, uint.max) for it then.

> 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

FYI, GSL (GNU Scientific Library) uses [0,1) for their uniform() and 
[0,n) for uniform_int().

-- Kenny.



More information about the Digitalmars-d mailing list