- #!/usr/bin/perl
-
my %sns = ();
-
for(my $i = 0;$i < 10; $i ++) {
-
my $range = 100;
-
do{
-
$no = rand($range);
-
$no = int($no);
-
$no = $no * 3 + 1;
-
-
}while($sns{"$no"} == 1);
-
$sns{"$no"} = 1;
-
print "$no\n";
-
-
}
随机产生10个3n+1的随机数,这10个数不重复,
用到了hash 来保存产生过的序列号,使得查找速度为O(1)
int(rand(100))为产生随机int
Over
阅读(9330) | 评论(0) | 转发(0) |