RandomSample with specified random number generator
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Tue Jun 12 06:07:42 PDT 2012
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.)
> 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
More information about the Digitalmars-d
mailing list