default random object?
Don
nospam at nospam.com
Sat Feb 14 05:09:32 PST 2009
Andrei Alexandrescu wrote:
> 4. While we're at it, should uniform(a, b) generate by default something
> in [a, b] or [a, b)? Someone once explained to me that generating [a, b]
> for floating point numbers is the source of all evils and that Hitler,
> Stalin and Kim Il Sung (should he still be alive) must be using that
> kind of generator. Conversely, generating [a, b) is guaranteed to bring
> in the long term everlasting peace to Earth. My problem however is that
> in the integer realm I always want to generate [a, b]. Furthermore, I
> wouldn't be happy if the shape of the interval was different for
> integers and floating point numbers. How to break this conundrum? Don't
> forget that we're only worrying about defaults, explicit generation is
> always possible with self-explanatory code:
>
> 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).
More information about the Digitalmars-d
mailing list