default random object?

Don nospam at nospam.com
Mon Feb 16 00:13:52 PST 2009


Andrei Alexandrescu wrote:
> Lionello Lunesu 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);
>>
>> I love your templates as much as the next guy, but let's not forget 
>> the possibility of using different function names instead of 
>> templating everything!?
> 
> I couldn't find good names. uniformOpenClosed, uniformOC, 
> uniformOpenLeft... what would be some good names?

I don't like that approach. The "[)", "[]" issue comes up every time 
that you have a numeric range. It's not just random number generation.

Unfortunately something like the following is too ugly.

uniform(rng, Interval!("[)")(a, b));

A more radical possibility would be to make .nextUp, .nextDown built-in 
primitives for floating-point types. Then you could make all the ranges 
"[]".

> 
>> And, as other have said, I'm ok with [) since that's what rand()%max 
>> boiled down to as well. Futhermore, since arrays in D are also [), 
>> users of D get used to it so it's not odd at all.
> 
> Ok. Let me just note that rand()%max is a lousy method of generating 
> random numbers between 0 and max-1 and everybody should put that in the 
> bin with Popular Examples That Should Never Be Used, together with 
> exponential Fibonacci, linear-space factorial, and bubblesort.
>
> 
> Andrei



More information about the Digitalmars-d mailing list