Ranges and random numbers -- again

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Jun 18 04:09:30 PDT 2013


On 06/18/2013 11:54 AM, Joseph Rushton Wakeling wrote:
> On 06/18/2013 11:31 AM, monarch_dodra wrote:
>> On Tuesday, 18 June 2013 at 10:16:02 UTC, Joseph Rushton Wakeling wrote:
>>> std.range.Take uses this constructor:
>>>
>>>     this(R input) { _original = input; _current = input.save; }
>>
>> It shouldn't. It is the caller who should chose to (or not to) save.
> 
> Ack, you're quite right.  Take doesn't even have a constructor -- that's a
> constructor for Cycle.

Proof that I was wrong -- please find attached a test module that implements a
final class (hence, reference type) version of Mersenne Twister.

If we then do,

    auto gen = new MtClass19937(unpredictableSeed);

    auto t1 = gen.take(5);
    auto t2 = gen.take(5);
    writeln(t1);
    writeln(t2);

... we get different sequences.  However, the .save as implemented needs
rewriting (I haven't touched it yet) as currently it simply returns a reference
copy.  I'll get onto that. :-)

Anyway, _assuming_ that we can rely on algorithms and ranges to not
surreptitiously .save the random number generator, this does seem to offer a
genuine alternative to my more problematic proposal.  I'm very happy about this
as I had feared my own idea was "least bad" rather than "best", never a nice
position to be in.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mtclass.d
Type: text/x-dsrc
Size: 5470 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130618/2cb1c835/attachment-0001.d>


More information about the Digitalmars-d mailing list