Ranges and random numbers -- again

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jun 18 07:38:20 PDT 2013


On Tue, Jun 18, 2013 at 03:32:02PM +0100, Joseph Rushton Wakeling wrote:
> On 06/18/2013 03:16 PM, H. S. Teoh wrote:
> > No, I just looked at the source code, and there is no call to .save in
> > the ctor. This is just another symptom of RNGs being passed by value: t1
> > and t2 are operating on two copies of rndGen passed by value. Were they
> > passed by reference, this wouldn't have been a problem.
> 
> You're right, it was my mistake -- I was scanning/searching the source code too
> quickly and a search for 'this(' took me into another struct without me realizing.
> 
> > Just make RNGs passed by reference, and the above problem will not
> > happen.
> 
> That was the conclusion I also came to after monarch_dodra's examples.  However,
> I'm still concerned about circumstances where an algorithm might choose to
> operate on a .save copy rather than on the original range -- this could result
> in unnoticed statistical correlations.
> 
> Can I reasonably assume that no D language/runtime/standard library algorithm
> would do that?  And if not, what are the examples where a .save is consumed
> instead of the original range?

Well, .save *is* used in a few places; the few that I know of include
cartesianProduct (at least one of the ranges need to be iterated over
repeatedly in order to compute the product), and find(), I believe,
where the needle needs to be at least a forward range so that multiple
candidate subranges can be checked repeatedly. And joiner forwards .save
to its wrapper range, though it doesn't actually call it as part of its
operation. There may be a few other places where .save is used.

W.r.t. (P)RNGs, the places where .save is used are where random ranges
wouldn't really make sense anyway -- if I *really* wanted to search a
range for a random needle, I'd make an array out of prng.take(n) first,
and then pass that to find(); it'd be more efficient, if nothing else.
And I can't think of any useful application of taking a cartesian
product of a random range.


T

-- 
"Hi." "'Lo."


More information about the Digitalmars-d mailing list