randomShuffle

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Mon Jun 3 05:28:06 PDT 2013


On 06/03/2013 10:48 AM, Yann wrote:
> I am trying to generate an array of 10 unique (!) random numbers from 1 to 1000
> each (for example). The best I could come up with is:

You mean you want a random sample of the numbers 1, 2, ..., 1000?

That is, you want to pick 10 unique numbers from 1, ..., 1000 with equal
probability of picking any one?

randomSample is your friend:

	auto arr = randomSample(iota(1, 1001), 10).array;

This picks a random sample of 10 values from the range iota(1, 1001).  (Note the
1001 is because iota iterates over [start, finish) not [start, finish], so you
need (1, 1001) if you want it to cover the values 1 to 1000.  I wish there was a
better/safer way of having a "closed-interval" iota.)


More information about the Digitalmars-d-learn mailing list