Perl有着强大的随机数产生函数rand(),下面的代码详细介绍其应用
#!/usr/bin/perl # use strict; use warnings; # 0~1之间 $random_number = rand();
print $random_number . "\n";
#大范围 $range = 100;
$random_number = rand($range);
print $random_number . "\n";
#整数 $range = 300;
$random_number = int(rand($range));
print $random_number . "\n";
#offset $range = 300; $minimum = 1;
$random_number = int(rand($range)) + $minimum;
print $random_number . "\n";
# 某范围间的随机整数 25,75 $random = int( rand( $Y-$X+1 ) ) + $X; #起止范围 $random = int( rand(51)) + 25; print "$random\n";
#随机密码
@chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(qin yang liu _ ! @ $ % ^ & *) ); $password = join("", @chars[ map { rand @chars } ( 1 .. 8 ) ]); print "\t$password\n";
#从数组生成随机密码
@array=qw,323 dd ddae oiljef 43,; $elt = $array[ rand @array ]; print "$elt\n"; $elt = $array[ int( rand(
0+@array) ) ]; print "$elt\n";
#随机密码,任意长度
sub randomPassword { my $password; my $_rand;
my $password_length = $_[0]; if (!$password_length) { $password_length = 10; }
my @chars = split(" ", "a b c d e f g h i j k l m n o p q r s t u v w x y z - _ % # | 0 1 2 3 4 5 6 7 8 9 秦");
srand;
for (my $i=0; $i <= $password_length ;$i++) { $_rand = int(rand 41); $password .= $chars[$_rand]; } return $password; }
#产生六位数的随机密码 my $password = randomPassword(6); # with 60 length print "password = ", randomPassword(60);