in reply to random index in array - no duplicates
That means that each element is used only once and that the resulting array is basically a randomized version of the input array.
And then scrunch up that code a little:#!/usr/bin/perl @arr=("1","2","3","4"); @sub=@arr; foreach (@arr){ $slot = int(rand(@sub)); ($new) = splice(@sub,$slot,1); push(@newarr,$new); } foreach (@newarr){ print "$_\n"; }
Still seems inelegant though . . . hmmmm.$hold=@arr; foreach (0...$hold){ push(@newarr, splice(@arr,int(rand(@arr)),1)); }
|
|---|