Class 13 — Wednesday February 12
You're so random
Look out my window — And see random acts of grace — By everyone
Look both ways
Agenda
- Introduce randomness
- Introduce Python module
random
- Introduce web processing
Feedback
- Please supply
For the fun of it
- Reveal one of your super power(s).
- Share a selfie.
- Sign up for peer mentoring
To do list
- Review class artifacts.
- Complete homework.
- Prepare for Test 1 (2 / 21 / 2020)
Feedback
- Please supply
Downloads
- Program pick_a_number.py
- Program chosen.py
- Program bit_by_bit.py
- Program octane.py
- Program master_plan.py
Being random
- How can we have an algorithm to generate truly random values?
- Short answer: we cannot
- But in practice: we can generate values that appear to be so random that can fool everyone (at least for now).
Module random
- Python has a standard module random with extensive capabilities for generating pseudo-random values.
- The module section of our website explains a subset of that module.
- There are three
random
module functions of particular interest to us now.
- Function
randrange()
: choose value from a range interval
- Function
choice()
: choose value from a string, list, set, or tuple
- Function
seed()
: specifically prime the random number generator
Program pick_a_number.py
- Demonstrates some simple usage of
random
module functionrandrange()
-
Some program runs
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 [ 0, 10 ): 4 4 8 8
Four random values from interval [ 0, 100 ): 22 64 2 80
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 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 ): 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 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
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
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 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
Web pages
Our introduction to interacting with the web is intentionally simple. Industrial-strength web applications also require familiarity with other and more powerful URL modules. There is an external library requests worth checking if you have further interest.
For now the only thing we is access to the module urllib.request
. The module supports working with URLs.
import urllib.request
- The only thing we care about in the modudle is its function
urllib.request.urlopen()
that returns a connector to a URL resource (think web page). Sample usage:
stream = urllib.request.urlopen( link )
- Officially the value returned by
urlopen()
is anhttp.client.HTTPResponse
.
- But all we care about is that a
stream
returned byurlopen()
has a functionread()
to get the contents of the web resource indicated bylink
.
page = stream.read()
- The contents provided by
read()
is a string encoded in a web format rather than as regular text. We can be decode it with string functiondecode()
.
text = page.decode( 'UTF-8' )
The above assignment sets text to be the decoded contents of the url resource named by
link
; that istext
is a string equally the contents of the url resource indicted by;ink
.
- The four statements form a template for getting the contents of a URL resource in string format.
import urllib.request # get module access
stream = urllib.request.urlopen( link ) # open connector to the link web resource
page = stream.read() # read contents of the resource
text = page.decode( 'UTF-8' ) # decode contents as normal text string
- What happens next is problem-dependent.
Program master_plan.py
- Displays the word of the day from the CS 1112 web file
word-of-the-day
.
???
© 2020 Jim Cohoon | Resources from previous semesters are available. |