sub randomElem {
my ($want, @array) = @_ ;
my (%seen, @ret);
while ( @ret != $want ) {
my $num = abs(int(rand($#array)));
if ( ! $seen{$num} ) {
++$seen{$num};
push @ret, $array[$num];
}
}
return @ret;
}
my @array = ( 'A'..'Z' );
for ( 1..10 ) { # 给你 random 10 个看看
my @new = randomElem ( 3 => @array ) ; # pick any 3 from @array
print "@new$/";
}
randomElem() 的第一个参数是你要从後面的数组中抽出几多个元素