随机产生10个3n+1的随机数,这10个数不重复,
用到了hash 来保存产生过的序列号,使得查找速度为O(1)
int(rand(100))为产生随机int
转自:http://blog.chinaunix.net/uid-24953145-id-1618683.html
#!/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"; }