请看看这个Perl random sampling code 有什么问题,# Programming - 葵花宝典
m*b
1 楼
想从一个array中随机抽样,比如,取两个.应当是常见问题.
#!/usr/local/bin/perl
#Define an Array and sampling size
my @myArray=('a','c','d','e','f','g','h','j','k','m','n','t');
my $numPick=2;
#use sub:selection_sample()
my @randPick=selection_sample(\@myArray,$numPick);
print (@randPick);
#### subroutine from perlmonk ####
sub selection_sample {
my ($array,$num)[email protected]_;
die "Too few elements (".scalar(@$array).") to select $num from\n"
unless $nummy @results;
my $pos=0;
while ( @results
#!/usr/local/bin/perl
#Define an Array and sampling size
my @myArray=('a','c','d','e','f','g','h','j','k','m','n','t');
my $numPick=2;
#use sub:selection_sample()
my @randPick=selection_sample(\@myArray,$numPick);
print (@randPick);
#### subroutine from perlmonk ####
sub selection_sample {
my ($array,$num)[email protected]_;
die "Too few elements (".scalar(@$array).") to select $num from\n"
unless $nummy @results;
my $pos=0;
while ( @results