randomSample

David Held via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 17 21:18:55 PDT 2014


How do I get an array from randomSample()?

int[] source = [ ... ];
int[] sample = randomSample(source, 3);

src\main.d(30): Error: cannot implicitly convert expression 
(randomSample(source, 3u)) of type RandomSample!(int[], void) to int[]

I get that RandomSample is a struct which implements the necessary 
interface to work with foreach.  But the fact that this struct is 
considered a proprietary implementation detail of std.random is a 
constant source of frustration for me.  Quite a few D libraries use this 
trick, but the opacity of these data structures make it impossible for 
users to know exactly how to use them outside of the one or two examples 
given in the documentation.  I don't know how to improve the situation, 
other than documenting them explicitly rather than treating them as a 
magical black box.

In the mean time, can anyone suggest a solution to the above, short of 
actually iterating over it?  I suppose someone will say to use map!().

Dave


More information about the Digitalmars-d-learn mailing list