# File: rand2.pl # Title: Generate a random string of length N (ask for N)over an alphabet # Author: Mihaela Malita # Generates a random string over Alphabet= A C G T # Length of the string? 10 # 0230111100 # Generated string is AGTACCCCAA @Alphabet= ('A','C','G','T'); # declare variable array Alphabet print "Generates a random string over Alphabet= @Alphabet"; print "\nLength of the string? "; $n = ; chop($n); $s = randstring(); #invokes subroutine print "\nGenerated string is $s"; exit; # use a subroutine $n is global variable sub randstring { my($s) = ''; # initialize final S with empty '' $len = @Alphabet; # length of the Alphabet (4) for (1..$n) { # $n is a global variable $die = int rand($len); # generates random 0 <= integer < L print $die; #ramdom index form Alphabet $s = $s . $Alphabet[$die]; # concatenation with . } return $s; }