default random object?

Denis Koroskin 2korden at gmail.com
Fri Feb 13 17:19:41 PST 2009


On Sat, 14 Feb 2009 04:10:29 +0300, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:

> Leonardo suggested that some functions in std.random should not require  
> their user to be bothered with creating a random object, i.e.:
>
> auto r = Random(unpredictableSeed);
> auto n = uniform(r, 0, 100);
>
> Instead the library should simply support:
>
> auto n = uniform(0, 100);
>

I think that both should be available.

> and do this by presumably using a global RNG under the hood. So I wanted  
> to ask all y'all:
>
> 1. Are you cool with making the rng the last parameter and give it a  
> default value?
>

Yes.

> 2. The global random generator will be allocated per thread. Are you  
> cool with this too?
>

Yes.

> 3. How should the global rng be initialized? To always generate the same  
> sequence, or not?
>

Yes, it should be deterministic.

> 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);
>

It should definitely include a but not b for floating point types.
It'd be satisfied if the same would be for integer types, too.

My typical use-case is "auto x = rand() % t;" which yields numbers in [0 to t).

For example, to get random range element, I'd like to write the following:
auto elem = range[uniform(0, range.length)];

>
> Thanks,
>
> Andrei






More information about the Digitalmars-d mailing list