I am considering this random string generator in perl:
sub generate_random_string {
my $length = 12;
my @chars = qw/2 3 4 5 6 7 8 9 A B开发者_开发百科 C D E F G H J K M N P Q R S T U V W X Y Z/;
my $str = '';
$str .= $chars[int rand @chars] for 1..$length;
return $str;
}
How many unique strings will this generate? If I extend the length of the string, how many more unique strings are available?
Also, how do I calculate the probability of generating the same string twice (assuming the length of the string stays at 12)?
The answer is: (1/31) ^ 12
Or more generically: (1/(number of characters)) ^ length
精彩评论