Ranges and random numbers -- again

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


On Tue, Jun 18, 2013 at 11:15:51AM +0100, Joseph Rushton Wakeling wrote:
> On 06/18/2013 10:30 AM, Joseph Rushton Wakeling wrote:
> > I don't come to that conclusion because I _want_ random ranges to be
> > un-.save-able, but because I think without that design choice, there
> > will simply be too many ways to unknowingly generate unwanted
> > correlations in random-number-using programs.
> > 
> > I'll follow up on that point later today.
> 
> Just as a simple example -- and this involving purely pseudo-random
> number generation, not "random ranges" as I've conceived them [*] --
> consider the following:
> 
>     auto t1 = rndGen.take(5);
>     writeln(t1);
> 
>     auto t2 = rndGen.take(5);
>     writeln(t2);
> 
> I'd expect that to produce two distinct sequences.  In fact it
> produces two identical sequences.
> 
> I think this must be because, passed a forward range like Mt19937,
> std.range.Take uses this constructor:
> 
>     this(R input) { _original = input; _current = input.save; }
> 
> ... and so when rndGen.take(5) is consumed, rndGen itself is not
> iterated forward.

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.

I say again that RNGs being passed by value is a major BUG. The above
situation is a prime example of this problem. We *need* to make RNGs
passed by reference. For situations where you *want* to duplicate a
pseudo random sequence, an explicit method should be provided to clone
the RNG. Duplication of RNGs should never be implicit.


[...]
> I'd be very happy to see a way in which this issue can be resolved
> while preserving the opportunity to .save, but I don't personally see
> one that doesn't rely on a heavy amount of user virtue to avoid these
> statistical pitfalls.
[...]

Just make RNGs passed by reference, and the above problem will not
happen.


T

-- 
Be in denial for long enough, and one day you'll deny yourself of things
you wish you hadn't.


More information about the Digitalmars-d mailing list