[Issue 10322] New: std.random.RandomSample.index() returns wrong value if called before front()
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jun 10 05:26:48 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10322
Summary: std.random.RandomSample.index() returns wrong value if
called before front()
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> 2013-06-10 05:26:46 PDT ---
RandomSample contains an index() property which returns the index value of the
currently selected item. So, if the currently-selected item is the 4th item
from the input range, index() should return 4.
However, if index() is called before the first call to front(), index returns
0. The attached example code shows this bug in action: the index value
_should_ in this example be identical to the value returned by front().
This bug is a regression caused by the fix to Issue #7936. Prior to that fix,
the first value of front() was determined in the constructor, so index() could
be guaranteed to be initialized: however, front(), and hence also index(),
cannot be initialized in the constructor because this would bias the
statistical independence of the sample.
The correct solution is therefore to ensure that front() and index() are both
initialized with the first call to either of them. Probably the simplest
solution is to ensure that index() calls front() internally, either always or
conditional on the private boolean variable _first.
So, either
index()
{
this.front;
return _index;
}
or,
index()
{
if(!_first)
this.front;
return _index;
}
Either will work, the question is which is preferable. :-)
--
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