Tutorial :How many random strings does this code generate?



Question:

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


Solution:1

The answer is: (1/31) ^ 12

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »