views:

87

answers:

1

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)?

+3  A: 

The answer is: (1/31) ^ 12

Or more generically: (1/(number of characters)) ^ length

Fosco
Thats 1.2e-18 or 1.2 quintillion possible strings.
snoopy