190 likes | 536 Views
Randomization. Randomization. Most computer languages have a built in function to generate "random" numbers The numbers generated are not truly random but are considered "pseudo-random" For most purposes, these are good enough. Randomization. The PERL Random number must be "seeded" first
E N D
Randomization • Most computer languages have a built in function to generate "random" numbers • The numbers generated are not truly random but are considered "pseudo-random" • For most purposes, these are good enough
Randomization • The PERL Random number must be "seeded" first • PERL will seed the rand function for you if you do not explicit seed the generator yourself • The command for seeding is srand(number); • Once the random number generator is seeded, it will generate a series of random numbers
Randomization • PERL's random numbers are called 'pseudo-random' because rand() will generate the same series of "random" numbers if the seed is the same • Getting the same series of random numbers can be useful for code testing • It can also be very dangerous when used for other purposes (generating random passwords)
Randomization • How to get a good seed? • The default seed is time() • If you do not explicitly call the srand() function, then PERL will call it for you the first time you use rand(). • PERL uses srand(time) and this is good enough for our purposes
Randomization • calling the rand function without an argument generates random numbers from 0 to 1 • example $x = rand(); print "x = $x \n"; output: x = 0.348843242566318
Randomization • calling the rand function with an argument generates random numbers from 0 to N • example $x = rand(10); print "x = $x \n"; output: x = 6.2584777484949
Randomization • if you want random integers, use the int() function • example $x = int rand(10); print "x = $x \n"; output: x = 4
Randomization • if you want a random element from an array, call the rand function using @list as the argument • in a "scalar context", @list returns the number of elements in the array • example @list = qw(dog cat fish); $index = int rand(@list); print "random = $list[$index] \n"; output: random = cat
Randomization • if you want a random element from an array, call the rand function using @list as the argument • example @list = qw(dog cat fish); print "random = $list[rand @list] \n"; output: random = fish Note: this works without int()
Randomization • array indices do NOT have to be integers! • $list[2.18171635] same as $list[2] • examples $list[2.9988494] same as $list[2] $list[2.1086304] same as $list[2] $list[0.9988494] same as $list[0] • PERL expects array indices to be integers • When it finds decimal numbers, it truncates • NOTE: truncate ≠ round • int() also truncates
Coin Toss Simulator • Random numbers are great for simulations • If you flip a coin 4 times, how often will you get 4 heads? => 0.5**4 = 0.0625 • Test this with PERL flip_coin 4 times => count if you get 4 heads repeat many times => report
Sequence Scrambler • Patterns occur in protein sequences • How can you tell if patterns are random? • A common test is to "scramble" a protein sequence and compare the native sequence to the scramble. • The scramble should have the same number of amino acids, the same molecular weight, pI and other properties. Only the order of the amino acids is changed.
Sequence Scrambler my @aas = split(//, $input); while(@aas){ my $index = int rand @aas; $scramble.= $aas[$index]; #select random aa splice(@aas, $index, 1); #remove selection } return $scramble;
Sequence Scrambler • Splice function allows you to take elements out of an array and/or replace them with other elements
Sequence Scrambler • Splice function allows you to take elements out of an array and/or replace them with other elements splice(@aas, $index, 1); removes 1 element from array @aas > $aas[$index] splice(@aas, $index, 3); removes 3 elements from array @aas $aas[$index], $aas[$index+1], $aas[$index+2]
Sequence Scrambler • Splice function allows you to take elements out of an array and/or replace them with other elements splice(@aas, $index, 1, 'A'); exchanges 1 element from array @aas $aas[$index] now contains 'A' splice(@aas, $index, 3, ('A', 'G', 'C', 'T')); exchanges 3 elements from array @aas and inserts 4
Sequence Scrambler • Splice function allows you to take elements out of an array and/or replace them with other elements