RandomSample with specified random number generator

Jens Mueller jens.k.mueller at gmx.de
Thu Jun 14 13:27:50 PDT 2012


Joseph Rushton Wakeling wrote:
> On 13/06/12 03:15, jerro wrote:
> >>Yes, I'll agree with you. But I don't know about others. It'll be nice
> >>if others share their opinion such that your efforts won't be wasted.
> >>That code was mostly written by Andrei and David.
> >>
> >>Jens
> >
> >I, for one, agree that this:
> >
> >>auto randomSample(R, Random)(R r, size_t n, Random gen =
> >>Random(unpredictableSeed))
> >
> >is the best way to solve this. The current implementation where
> >Random can be void and then RandomSample uses the global rng and
> >everything behaves slightly differently just seems like an endless
> >source of bugs.
> 
> I've filed a bug report relating to this.
> http://d.puremagic.com/issues/show_bug.cgi?id=8247

Thanks.

> Assuming API changes are permissible (a big IF), I think the way to
> do it might be something like
> 
> auto randomSample(R, UniformRNG = Random, seed)(R r, size_t n, seed
> s = unpredictableSeed)
> 
> ... meaning that the user has an option to specify an RNG _type_
> without specifying a seed, and that the seed can be either
> unpredictable or specific.
> 
> The user could also not specify a RNG type, in which case the
> default RNG type would be used, but would be seeded unpredictably.

Why don't you want to pass an instance of some RNG?
Then there are two places to change when you want to change generated
random numbers.

What are the advantages of the above in regard to
auto randomSample(R, Random)(R r, size_t n, Random r = Random(unpredictableSeed))
?
Is it because you got bitten that the generators are passed by value?

Jens


More information about the Digitalmars-d mailing list