RandomSample with specified random number generator

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


Joseph Rushton Wakeling wrote:
> On 12/06/12 13:46, Jens Mueller wrote:
> >Probably I'm not seeing the issue
> >
> >auto sample3 = randomSample(iota(0, 100), 5, Random(unpredictableSeed));
> >writeln(sample3);
> >auto sample4 = randomSample(iota(0, 100), 5, Random(unpredictableSeed));
> >writeln(sample4);
> >auto sample5 = randomSample(iota(0, 100), 5, Random(unpredictableSeed));
> >writeln(sample5);
> >
> >outputs
> >
> >[21, 38, 57, 86, 90]
> >[21, 39, 68, 79, 94]
> >[21, 22, 57, 86, 92]
> 
> Yes, because you're passing it each time a new RNG seeded with an
> unpredictable seed :-)
> 
> Try instead
> 
>    auto rng = Random(unpredictableSeed)
>    auto sample3 = randomSample(iota(0, 100), 5, rng);
>    writeln(sample3);
>    auto sample4 = randomSample(iota(0, 100), 5, rng);
>    writeln(sample4);
>    auto sample5 = randomSample(iota(0, 100), 5, rng);
>    writeln(sample5);
> 
> ... 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.

> >Besides the fact that the ranges always contain 21 (a bug?) this looks
> >fine to me, doesn't it?
> 
> The first-sample-point issue is a bug which I fixed in a recent pull request:
> https://github.com/D-Programming-Language/phobos/pull/553#issuecomment-5608813

I see.

Jens


More information about the Digitalmars-d mailing list