default random object?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Feb 14 06:24:14 PST 2009


Don wrote:
> Andrei Alexandrescu wrote:
>> auto rng = Random(unpredictableSeed);
>> auto a = 0.0, b = 1.0;
>> auto x1 = uniform!("[]")(rng, a, b);
>> auto x2 = uniform!("[)")(rng, a, b);
>> auto x3 = uniform!("(]")(rng, a, b);
>> auto x4 = uniform!("()")(rng, a, b);
> 
> This is a general issue applying to any numeric range. I've been giving 
> the issue of numeric ranges some thought, and I have begun an 
> implementation of a general abstraction.
> Any open range can be converted into a closed range, but the converse 
> does not apply. So any implementation will be using "[]" internally.
> 
> -range("[)", a, b) == range("(]", -b, -a)
> range("[)", a, b) == range("[]", a, predecessor(b))
> range("()", a, b) == range("[]", successor(a), predecessor(b))
> 
> 
> There's a couple of difficult situations involving floating-point numbers.
> *  "[)" has the uncomfortable property that (-2,-1, rng) includes -2 but 
> not -1, whereas (1, 2, rng) includes 1 but not 2.
> 
> * any floating point range which includes 0 is difficult, because there 
> are so many numbers which are almost zero. The probability of getting a 
> zero for an 80-bit real is so small that you probably wouldn't encounter 
> it in your lifetime. I think this weakens arguments based on analogy 
> with the integer case.
> 
> However, it is much easier to make an unbiased rng for [1,2) than for 
> [1,2] or (1,2) (since the number of members in the range is even).

So what would you recommend? [a, b) for floats and [a, b] for ints, or 
[a, b) for everything?

Andrei



More information about the Digitalmars-d mailing list