randomness-11

randomness-11 - Keith O'Hara [email protected] xkcd.com Oct...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Keith O'Hara [email protected] xkcd.com Oct 13 2010 CMSC143 - O'Hara 1 Randomnes s CMSC 143 Announcements Scribbler Status Assignment Midterm Min. 61.50 1st Qu. 83.75 Median 89.50 Mean 86.28 3rd Qu. 93.62 Max. 98.50 Oct 13 2010 CMSC143 - O'Hara 2 Analyzing FDR's Speech Import data into python Count number of characters, words, and sentences Spell check Find most and least used words Find reoccurring metaphors Check for plagiarism Maybe not in this case President Hoover, Mr. Chief Justice, my friends: This is a day of national consecration, and I am certain that my fellowAmericans expect that on my induction into the Presidency I will address them with a candor and a decision which the present situation of our nation impels. This is preeminently the time to speak the truth, the whole truth, frankly and boldly. Nor need we shrink from honestly facing conditions in our country today. This great nation will endure as it has endured, will revive and will prosper. So first of all let me assert my firm belief that the only thing we have to fear. . .is fear itself. . . nameless, unreasoning, unjustified terror which paralyzes needed efforts to convert retreat into advance. Oct 13 2010 CMSC143 - O'Hara ... 3 Counting Individual Words How could we track how many times each word is used? The: 120 We: 7 Have: 20 Find the words most used, least used? Dictionary! Oct 13 2010 CMSC143 - O'Hara 4 Dictionaries Very useful (mutable) compound data type Key/Value pairs Can be indexed by an immutable type (int, string, float) wordcount = {`the': 184, `fear': 2, ...} print "FDR said the", wordcount[`the'], "times" print "There were", len(wordcount), "number of distinct words" wordcount[`a'] = wordcount[`a'] + wordcount[`A'] del wordcount[`A'] print wordcount[`computer'] #key error Oct 13 2010 CMSC143 - O'Hara 5 Iterating over a Dictionary Use for to traverse a dictionary Loop variable is the "key" Use "in" keyword to see if a key is in a dictionary for word in wordcount: print word, wordcount[word] `the' in wordcount # evals to True `computer' in wordcount # evals to False Oct 13 2010 CMSC143 - O'Hara 6 xkcd.com Oct 13 2010 CMSC143 - O'Hara 7 A Hint of Scribbler Free Will? >>> import soul Oct 13 2010 CMSC143 - O'Hara 8 "Random" Without Purpose Without Cause NonDeterministic Unpredictable or hard to predict Follows Some Probability Distribution Oct 13 2010 CMSC143 - O'Hara 9 A Hint of Scribbler Free Will? from myro import * init() while timeRemaining(10): l = randomNumber() r = randomNumber() motors(l, r) stop() Never goes backwards? Oct 13 2010 CMSC143 - O'Hara 10 Pseudo-Free Will Pseudo-Random Deterministically generated Exhibits "randomness" in the statistical sense import random # random number [0-1) random.seed(1234) print random.random() print random.random() random.seed(1234) print random.random() "Seed" is the initial value Oct 13 2010 CMSC143 - O'Hara 11 Random Numbers Random numbers can be useful in many applications: Statistics Simulation Games Robotics Graphics Machine Learning Evolutionary Computing Cryptography Oct 13 2010 CMSC143 - O'Hara from myro import * # random number # between [0,1) print randomNumber() # heads or tails? print flipCoin() 12 Self-Driving Cars & SLAM Simultaneous Localization and Mapping http://www.nytimes.com/2010/10/10/science/10googl Oct 13 2010 CMSC143 - O'Hara e.html 13 Randomness in Robotics Monte Carlo Localization Robot: "Where am I" Particle Filter Each particle represents one of the robot's possible positions Areas without particles have low probability Oct 13 2010 CMSC143 - O'Hara 14 Creating Our Own Random #'s How can we use our sensors to create a real random number generator? How could we test how random it is? How could we generate random integers? Oct 13 2010 CMSC143 - O'Hara 15 def flipRobotCoin(): # our function here # maybe use getLight() Scribbler Music What's wrong here? import random for i in range(10): dur = random.random() freq = random.random() beep(dur, freq) Oct 13 2010 CMSC143 - O'Hara 16 Random Number from 0 to 600 Want a random number between 0 and 600? Remember: random() ranges from 0 up to (but not including) 1 rv = 600 * random.random() 0 .9999 0 Oct 13 2010 CMSC143 - O'Hara 599.9999 17 Scribbler Music Random frequencies between 0600 import random for i in range(10): dur = random.random() freq = 600 * random.random() beep(dur, freq) Oct 13 2010 CMSC143 - O'Hara 18 Random Number from 400 to 600 Want a random number between rv 400 and 600? = 400 + 200 * random.random() 0 .9999 400 Oct 13 2010 CMSC143 - O'Hara 599.9999 19 Random Number from x to y What if we want a random number between x and y? 0 .9999 def randNum(x, y): width = (y - x) rv = x + width * random.random() return rv x Oct 13 2010 CMSC143 - O'Hara y 20 Scribbler Music Random frequencie s between 400-600 import random low = 400 high = 600 for i in range(10): dur = random.random() width = high - low freq = low + (width * random.random()) beep(dur, freq) Oct 13 2010 CMSC143 - O'Hara 21 Scribbler Music Shortcut Random frequencie s between 400-600 random. uniform(lo w, high) import random low = 400 high = 600 for i in range(10): dur = random.random() freq = random.uniform(low, high) beep(dur, freq) Oct 13 2010 CMSC143 - O'Hara 22 Still Sounds Bad Why? Oct 13 2010 CMSC143 - O'Hara 23 Other Random Functions random. uniform(low, high) Float: low <= x < high random.randint(low,high) Integer: low <= x <= high random.choice(sequence) Random element of a sequence random.shuffle(sequence) Randomly shuffle a sequence Oct 13 2010 CMSC143 - O'Hara 24 Friendlier Scribbler Music Random notes Use `choice' function How can we make some notes more likely? import random notes = [440, 494, 523, 587, 659] for i in range(10): dur = random.random() freq = random.choice(notes) beep(dur, freq) Oct 13 2010 CMSC143 - O'Hara 25 Speaking Like FDR? Randomly generate a speech from the words of his inaugural address Random letters Random words Random pairs of words Oct 13 2010 CMSC143 - O'Hara 26 Speaking More Like FDR Build a table of Bigrams 2 words that occur together word["the"] = ["country", "congress"] Randomly generate from that table choice[words["the"]] "money changers have failed through this great as the truth, frankly and willing to work. This is to take one of communications and action now. Our greatest primary task as we apply social values more money. Stripped of the pattern of executive and there must be that guides these courses," Oct 13 2010 CMSC143 - O'Hara 27 ...
View Full Document

This note was uploaded on 10/14/2010 for the course CMSC 143 taught by Professor Keitho'hara during the Spring '10 term at Bard College.

Page1 / 27

randomness-11 - Keith O'Hara [email protected] xkcd.com Oct...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online