1 / 18

Randomization

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

kiet
Télécharger la présentation

Randomization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Randomization

  2. 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

  3. 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

  4. 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)

  5. 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

  6. 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

  7. 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

  8. Randomization • if you want random integers, use the int() function • example $x = int rand(10); print "x = $x \n"; output: x = 4

  9. 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

  10. 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()

  11. 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

  12. 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

  13. 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.

  14. 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;

  15. Sequence Scrambler • Splice function allows you to take elements out of an array and/or replace them with other elements

  16. 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]

  17. 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

  18. Sequence Scrambler • Splice function allows you to take elements out of an array and/or replace them with other elements

More Related