Randomization - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Randomization

Description:

Most computer languages have a built in function to generate 'random' numbers. The numbers generated are not truly random but are considered 'pseudo-random' ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 19
Provided by: MartinS48
Category:

less

Transcript and Presenter's Notes

Title: Randomization


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 _at_list as the argument
  • in a "scalar context", _at_list returns the number
    of elements in the array
  • example
  • _at_list qw(dog cat fish)
  • index int rand(_at_list)
  • print "random listindex \n"
  • output random cat

10
Randomization
  • if you want a random element from an array, call
    the rand function using _at_list as the argument
  • example
  • _at_list qw(dog cat fish)
  • print "random listrand _at_list \n"
  • output random fish
  • Note this works without int()

11
Randomization
  • array indices do NOT have to be integers!
  • list2.18171635 same as list2
  • examples
  • list2.9988494 same as list2
  • list2.1086304 same as list2
  • list0.9988494 same as list0
  • 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? gt 0.54 0.0625
  • Test this with PERL
  • flip_coin 4 times gt count if you get 4 heads
  • repeat many times gt 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 _at_aas split(//, input)
  • while(_at_aas)
  • my index int rand _at_aas
  • scramble. aasindex select
    random aa
  • splice(_at_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(_at_aas, index, 1)
  • removes 1 element from array _at_aas gt aasindex
  • splice(_at_aas, index, 3)
  • removes 3 elements from array _at_aas
  • aasindex, aasindex1, aasindex2

17
Sequence Scrambler
  • Splice function allows you to take elements out
    of an array and/or replace them with other
    elements
  • splice(_at_aas, index, 1, 'A')
  • exchanges 1 element from array _at_aas
  • aasindex now contains 'A'
  • splice(_at_aas, index, 3, ('A', 'G', 'C', 'T'))
  • exchanges 3 elements from array _at_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
Write a Comment
User Comments (0)
About PowerShow.com