Class 17 — Friday October
You're so random
Like to look around — And see random acts of grace — By everyone
Look both ways
Agenda
- Introduce randomness using Python module
random
- Continue with data set processing
Download
- Program chosen.py
- Program pick_a_number.py
- Program bit_by_bit.py
- Program octane.py
To do list
- Review class artifacts.
- Complete homework
- Check out random module
Being random
- Python has a standard module random with extensive capabilities for generating pseudo-random values.
- Why say pseudo-random? Since we are using algorithm there is some determinism involved.
- There are algorithms that can generate values that appear to be so random that can fool everyone (at least for now).
- There are three
random
module functions of particular interest to us now.
- Function
choice()
: choose value from a string, list, set, or tuple
- Function
seed()
: specifically prime the random number generator
- Function
randrange()
: choose value from a range interval
Program chosen.py
- Demonstrates some simple usage of
random
module functionchoice()
-
Some program runs
Enter a string: abcdefghij
Four random characters from string abcdefghij : e a j d
Enter some words: th fo ju ov th la lo
Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : ov lo la ov
Enter a string: abcdefghij
Four random characters from string abcdefghij : a i b h
Enter some words: th fo ju ov th la lo
Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : th fo th ju
Enter a string: simultaneously
Four random characters from string simultaneously : i y m t
Enter some words: 31 41 597 2 71
Four random words from list ['31', '41', '597', '2', '71'] : 71 597 31 71
Program pick_a_number.py
- Demonstrates some simple usage of
random
module functionrandrange()
-
Some program runs
Enter four integers: 10 20 75 100
Four random values from interval [ 10, 20 ): 14 14 18 18
Four random values from interval [ 75, 100 ): 82 75 92 80
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 7 4 3 5
Four random values from interval [ 0, 100 ): 15 28 64 87
Enter four integers: 10 20 75 100
Four random values from interval [ 10 , 20 ): 11 11 19 15
Four random values from interval [ 75 , 100 ): 89 85 76 99
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 1 2 8 6
Four random values from interval [ 0, 100 ): 57 63 81 18
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 3 5 3 6
Four random values from interval [ 0, 100 ): 29 12 32 5
Enter four integers: 10 20 75 100
Four random values from interval [ 10 , 20 ): 13 14 16 19
Four random values from interval [ 75 , 100 ): 92 98 75 75
Enter four integers: 0 8 31 39
Four random values from interval [ 0 , 8 ): 7 2 1 3
Four random values from interval [ 31 , 39 ): 37 36 32 37
Enter two integers: 2 10
Four random values from interval [ 0, 2 ): 1 0 0 1
Four random values from interval [ 0, 10 ): 6 6 2 8
Program bit_by_bit.py
— generating random lists of values
- Generates a list of
n
bits (binary digits) for a user-suppliedn
.
-
Some possible program runs
How many bits: 8
[1, 0, 0, 0, 0, 0, 1, 1]
How many bits: 8
[1, 0, 0, 1, 1, 0, 0, 1]
How many bits: 28
[0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1]
How many bits: 28
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1]
Program octane.py
- Prompts and gets three user-supplied integers
s
,n
, andd
. Uses integers
as a seed to the Python random number generator.
- Accumulates a list of
n
octal digits (base 8 digits; that is digits from the interval 0 to 7) one by one. Afterwards, prints the list of octal digits.
- Determines and prints the number of occurrences of
d
in the list of octals (check out the list module for a helpful function.
-
Some possible program runs
Enter three numbers: 1112 12 1
octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]
number of times value 1 in list: 4
Enter three numbers: 1112 12 5
octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]
number of times value 5 in list: 2
Enter three numbers: 8 9 6
octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]
number of times value 6 in list: 1
Enter three numbers: 8 9 6
octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]
number of times value 3 in list: 3