default random object?

Bill Baxter wbaxter at gmail.com
Sun Feb 15 10:50:13 PST 2009


On Mon, Feb 16, 2009 at 3:41 AM, dsimcha <dsimcha at yahoo.com> wrote:
> == Quote from Bill Baxter (wbaxter at gmail.com)'s article
>> On Mon, Feb 16, 2009 at 2:33 AM, Don Clugston <nospam at nospam.com> wrote:
>> > Andrei Alexandrescu wrote:
>> >>
>> >> 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
>> >
>> > I'm leaning towards [a,b) for everything (consistency with arrays), but I'd
>> > want to know what the reasoning of the boost/c++0x guys was.
>> How do you create a random uint that can take on any of uint's values
>> with [a,b)?  That's the main reason I can think of to go with [a,b]
>> for integral types.  With floats it's never useful to use the entire
>> value range.
>> --bb
>
> Keep in mind that we're talking only about defaults here.  At least in the current
> implementation, "[)" vs. "[]" can be specified by template parameters.  Wanting
> the entire range of an integer type is an edge case, so it's not important that it
> be well-supported by the defaults, as long as it can be done without resorting to
> serious kludges.

I don't agree that it's such an edge case.  I often want a random
ubyte that covers the entire range.

--bb



More information about the Digitalmars-d mailing list