Trying to understand RandomSample struct in std.random

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Apr 12 11:30:47 PDT 2012


Hello all,

I'm trying to understand the internal operations of the RandomSample struct in 
std.random.

What gets output at the end is clearly an array containing a subset of the 
original input.  But I can't understand how this is constructed.

The constructor sets various initial values and then calls a function prime() 
[looks like an implementation of Knuth's Algorithm S] which returns when the 
current _index value is selected.  Otherwise, it moves along to the next index 
value.

What I don't understand is where the selected values are stored or how they come 
to be available.

I also don't understand the corresponding randomSample functions where a 
specified random number generator is passed; how it can work to set ret.gen = 
gen _after_ the RandomSample struct has been initialized.

Can anyone help me to understand how this struct works?

Thanks & best wishes,

     -- Joe


More information about the Digitalmars-d-learn mailing list