RandomSample with specified random number generator

Jens Mueller jens.k.mueller at gmx.de
Tue Jun 12 06:58:02 PDT 2012


Joseph Rushton Wakeling wrote:
> On 12/06/12 14:28, Jens Mueller wrote:
> >>... and you'll get out each time the same values.  (Or instead of a
> >>newly-defined generator you could just use rndGen as in my code
> >>examples.)
> >
> >Of course. But this behavior is clear from the documentation. Did it
> >surprise you? Passing a rng by value is fine I think.
> 
> _Is_ it clear?  I would expect that if I create and use 3 samples as
> in my example code, then I would get 3 _different_ samples.  But
> perhaps I'm too used to a C-style past where a single RNG is passed
> around to different functions within the thread.
> 
> Bear in mind that the behaviour is different depending on whether
> you pass an RNG or not.  i.e. if you do
> 
>     sample = randomSample(iota(0, 100), 5);
>     writeln(sample);
>     writeln(sample);
>     writeln(sample);
> 
> ... you'll get 3 _different_ sets of values, whereas if you do
> 
>     sample = randomSample(iota(0, 100), 5, Random(unpredictableSeed));
>     writeln(sample);
>     writeln(sample);
>     writeln(sample);
> 
> ... you'll get out 3 times the same sample.
> 
> This could be resolved by making it so that if RandomSample is not
> passed a specified RNG, it sets the internally-stored RNG to be
> Random(unpredictableSeed).

Right. These are inconsistent. This should be fixed. Can't we just use a
default argument like
auto randomSample(R, Random)(R r, size_t n, Random gen = Random(unpredictableSeed));

Currently the documentation does not even state what is difference
between the versions accepting a random generator vs. the ones without a
random generator.

Jens


More information about the Digitalmars-d mailing list