开发者

How many random strings does this code generate?

开发者 https://www.devze.com 2023-01-08 12:51 出处:网络
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/;

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

0

精彩评论

暂无评论...
验证码 换一张
取 消