Ranges and random numbers -- again

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Jun 18 08:47:14 PDT 2013


On 06/18/2013 04:26 PM, Joseph Rushton Wakeling wrote:
> ... and the results keep changing.  The first argument to cartesianProduct is
> not consumed, the second is.

I guess it's down to this part of cartesianProduct:

    static if (isInfinite!R1 && isInfinite!R2)
    {
        ...
    }
    else static if (isInputRange!R2 && isForwardRange!R1 && !isInfinite!R1)
    {
        return joiner(map!((ElementType!R2 a) => zip(range1.save, repeat(a)))
                          (range2));
    }
    else static if (isInputRange!R1 && isForwardRange!R2 && !isInfinite!R2)
    {
        return joiner(map!((ElementType!R1 a) => zip(repeat(a), range2.save))
                          (range1));
    }

In the case where both R1 and R2 are finite forward ranges, cartesianProduct
will save range1 and not range2.

I accept that this may not be an area where random input is expected, but it
still feels "ouch" and is the kind of unexpected correlation problem I had
anticipated.

If we remove the .save property from MtClass19937 then the problem vanishes, but
it is a shame to have to lose .save for pseudo-random number generators.


More information about the Digitalmars-d mailing list