Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Generating a random string with Perl

  • Public
By Abhinav 2888 days ago Comments (1)
#!/usr/bin/perl # This function generates random strings of a given length sub generate_random_string { my $length_of_randomstring=shift;# the length of # the random string to generate my @chars=('a'..'z','A'..'Z','0'..'9','_'); my $random_string; foreach (1..$length_of_randomstring) { # rand @chars will generate a random # number between 0 and scalar @chars $random_string.=$chars[rand @chars]; } return $random_string; } #Generate the random string my $random_string=&generate_random_string(11); print "Random string: ".$random_string."\n"; print "Length: ".length($random_string)."\n";

Comments

  • Jit 2888 days ago

    Hmmm interesting, but I prefer Perl module String::Random;

    use String::Random;
    my $pass = String::Random->new;
    print "Your password is ", $pass->randpattern("CCcc!ccn"), "\n";

    This would output something like this:

    Your password is UDwp$tj5