Generating Random string of length n.

While solving programming puzzle you will need to generate lot of test cases.  Here is the small Perl script to do the same.
This script will generate a string of random characters of the specified length.

#!/usr/bin/perl
sub generate_random_string {
    my $length_of_randomstring=shift;
    my @chars=('a'..'z');
    my $random_string;
    foreach (1..$length_of_randomstring) {
        $random_string.=$chars[rand @chars];
    }
    return $random_string;
}
my $random_string = &generate_random_string(2000);
print "Random string: ".$random_string."\n";
print "Length: ".length($random_string)."\n";
Advertisements

One thought on “Generating Random string of length n.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s