Trying to understand RandomSample struct in std.random

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Apr 12 13:27:12 PDT 2012


On 12/04/12 20:39, Ali Çehreli wrote:
> That's misleading. RandomSample is a lazy range. The output seems to be elements
> of an array only as you pull data out of this range.

Ahhh, it's lazy evaluation.  That would explain why you can set ret.gen = gen in 
the randomSample functions and have it work.

(... realized that before reading your explicit statement to that effect:-)

> I shamelessly provide a link to a chapter that I have written, which I think
> covers all of these topics:
>
> http://ddili.org/ders/d.en/ranges.html

No shame required, your textbook is very nice :-)

I think I understand better now -- that the initial value is calculated by the 
call to prime() in the constructor, which leaves _input.front as being the first 
selected value; and then popFront() calls prime() again to set the next value, 
and so on.

What I _still_ don't understand is the statement in the constructor:

     // we should skip some elements initially so we don't always
     // start with the first

... as it seems to me that doing so would bugger up the selection algorithm. 
The whole point of Algorithm S is that it considers one by one each of the 
possible values in sequence.


More information about the Digitalmars-d-learn mailing list