[Issue 8247] New: Inconsistent behaviour of randomSample depending on whether a random number generator is specified
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 14 12:25:41 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8247
Summary: Inconsistent behaviour of randomSample depending on
whether a random number generator is specified
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: joseph.wakeling at webdrake.net
--- Comment #0 from Joseph Rushton Wakeling <joseph.wakeling at webdrake.net> 2012-06-14 12:27:53 PDT ---
Created an attachment (id=1116)
Working minimal example illustrating the inconsistencies described.
The randomSample function in std.random can be called with or without
specifying a random number generator to use.
If no RNG is specified, then each lazy evaluation of the sample evaluates
differently, i.e. if you do
sample1 = randomSample(iota(0, 100), 5);
writeln(sample1);
writeln(sample1);
writeln(sample1);
you will get 3 different samples.
Conversely, if a random number generator is specified, you will get 3 times the
same result:
sample2 = randomSample(iota(0, 100), 5, Random(unpredictableSeed));
writeln(sample2);
writeln(sample2);
writeln(sample2);
Note that the seeding of the RNG is important, because if an already-existing
RNG is provided to create multiple different samples, they will evaluate
identically, e.g.
sample3 = randomSample(iota(0, 100), 5, rndGen);
writeln(sample3);
sample4 = randomSample(iota(0, 100), 5, rndGen);
writeln(sample4);
sample5 = randomSample(iota(0, 100), 5, rndGen);
writeln(sample5);
... will produce the same output 3 times. This happens because the RNG passed
to randomSample is copied rather than used by reference.
These inconsistencies lead to a lot of potential confusion and sources of bugs.
So, first of all, we need a firm decision on how the lazy evaluation of
RandomSample should behave -- should it
(1) always evaluate to the same sample, or
(2) always evaluate to a different sample?
... and depending on the answer, we then need to address how to specify and
seed an RNG for RandomSample.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list