in reply to Re: Picking unique element in random from a array
in thread Picking unique element in random from a array
Several ways to do this that involve changing the original array are to use List::Util's shuffle() as davorg has suggested or use splice to pull out elements randomly. Alternatively, a parrallel array of indices allows the original array to remain intact.
my (@index, @rand); @index = 0 .. $#array; push @rand, splice(@index, rand @index, 1) for 1 .. 10; print "@array[@rand]\n"; # untested
Update: I added a couple of alternatives that do not leave the original array intact. Additionally, I need to point out that your use of Data::Random can be terribly runtime inefficient. See my update in this node for details. The alternatives consume more memory (trading space for time). I also originally incorrectly thought that the module was returning the set in the original order by default but it isn't.
Cheers - L~R
|
|---|