CS 121 Today Fractals and Turtles! The Koch Curve how random…

Slides:



Advertisements
Similar presentations
ABC Welcome to the Monty Hall show! Behind one of these doors is a shiny new car. Behind two of these doors are goats Our contestant will select a door.
Advertisements

Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Homework 4 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/29 Mon., 9/30 Problems
Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:
What is the probability that it will snow on Christmas day in Huntingdon?
EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University
Cyriak: conceptually disruptive recursion… Baaa. Welcome to IST338… Be sure to watch your head!
CS 5 Today CS 5 alien on strike! Claremont residents report droplets of water falling down-ward from the sky: unexplained meteorological phenomenon causes.
Welcome to CS 5! Be sure to watch your head…. A recursive spiral …
Independence and Dependence 1 Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University.
IS 313 Tomorrow… IS 313 last week ? 9/20/09 - today: beyond recursion! 9/27/09 - next wk: web technologies, pt 2 Assignment reminders... Which door to.
CSCE 2100: Computing Foundations 1 Probability Theory Tamara Schneider Summer 2013.
STAT115 STAT225 BIST512 BIO298 - Intro to Computational Biology Python Tutorial II Monty Python, Game of Life and Sequence Alignment Feb 1, 2011 Daniel.
“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how.
The not-so-subtle art of singling out the best (and worst) of anything… Computing with language Computing to the max You know this would make me hungry…
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University
CS 121 Today Fractals and Turtles! The Koch Curve how random…
IS 313 Tomorrow… IS 313 Today? 9/16/09 - today: recursion and beyond! 9/23/09 - next wk: no meeting (DC) 9/30/09 - following wk: for & while Homework functions.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Introduction to Discrete Probability Epp, section 6.x CS 202.
Code Compression the benefits of looping... Today in CS 5 HW 4 - (3 problems) M/T sections W/Th sections due Sunday, 9/26 at midnight due Monday, 9/27.
Coding Time This is a starter activity and should take about 10 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.Start a script session (Select.
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
Probability Test Review (What are your chances of passing?)
Ray Karol 2/26/2013. Let’s Make a Deal Monte Hall Problem Suppose you’re on a game show, and you’re given a choice of three doors: Behind one door is.
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
Python Basics.
Cyriak: conceptually disruptive recursion…
Chapter 5: Bayes’ Theorem (And Additional Applications)
EECS 110: Lec 10: Definite Loops and User Input
CSc 110, Spring 2017 Lecture 12: Random Numbers
Introduction to Probability Distributions
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
CS005 Introduction to Programming
Modelling and Simulating Social Systems with MATLAB
EECS 110: Lec 7: Program Planning
EECS 110: Lec 6: Fractals and Trutles
CSc 110, Autumn 2016 Lecture 13: Random Numbers
While Loops in Python.
EECS 110: Lec 4: Functions and Recursion
The Monty Hall Problem Madeleine Jetter 6/1/2000.
Random numbers Taken from notes by Dr. Neil Moore
Writing Functions( ) (Part 5)
Monty Hall This is a old problem, but it illustrates the concept of conditional probability beautifully. References to this problem have been made in much.
Preconditions, Postconditions & Assertions
WARM - UP The American Red Cross says that about 45% of the US Population has Type O blood, 40% Type A, 11% Type B, and the rest Type AB. a.) Selecting.
Random numbers What does it mean for a number to be random?
Random numbers What does it mean for a number to be random?
Pick a number, any number …
Suppose you roll two dice, and let X be sum of the dice. Then X is
Probability And Expected Value ————————————
Building Java Programs
EECS 110: Lec 4: Functions and Recursion
Probability, Games & Sentiment Analysis
Probability And Expected Value ————————————
CS150 Introduction to Computer Science 1
Nate Brunelle Today: Conditional Decision Statements
The Monty Hall Game PLAY Teacher’s Notes.
Randomized Algorithms
Let’s Win a Lollipop! Door 1 Door 2 Door 3 Win! You pick...and stay with and the Lose.   You pick...and stay with... Door 1 Door 2 Door 3 ...and.
While Loops in Python.
Random numbers What does it mean for a number to be random?
Recursion Exercises.
Randomized Algorithms
Presentation transcript:

CS 121 Today Fractals and Turtles! The Koch Curve how random…

When good programs go bad… def power(b, p): """ Returns b**p for p >= 0 """ if p == 0: return 1 else: return b*power(b, p)

print: Making programs talk to you! Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. - Maurice Wilkes Programming: the art of debugging an empty file. - The Jargon File

When good programs go bad… def power(b, p): """ Returns b**p for p >= 0 """ print ("p is", p, "; b is", b) if p == 0: return 1 else: return b*power(b, p)

Careful! print != return def power(b, p): """ Returns b**p for p >= 0 """ if p == 0: return 1 else: return b*power(b, p-1) def powerPrint(b, p): """ Returns(?) b**p for p >= 0 """ if p == 0: print (1) else: print (b*powerPrint(b, p-1))

sum, range def sum(L): """ input: a list of numbers, L output: L's sum """

sum, range def sum(L): """ input: a list of numbers, L output: L's sum """ if len(L) == 0: return 0.0 else: return L[0] + sum(L[1:]) Base Case if the input has no elements, its sum is zero Recursive Case if L does have an element, add that element's value to the sum of the REST of the list… This input to the recursive call must be "smaller" somehow…

sum, range def range(low,hi): """ input: two ints, low and hi output: int list from low up to hi """ excluding hi

sum, range def range(low,hi): """ input: two ints, low and hi output: int list from low up to hi """ if hi <= low: return [] else: return excluding hi

sum, range def range(low,hi): """ input: two ints, low and hi output: int list from low up to hi """ if hi <= low: return [] else: return [low] + range(low+1,hi) excluding hi

A random aside… import random random.choice( L ) random.uniform(low,hi) random.choice( ['north', 'case', 'west'] ) random.uniform(41.9,42.1) chooses 1 element from the list L (with uniform probability) chooses a random float from low to hi for more explanation, try dir(random) or help(random) How likely is this to return 42 ? How would you get a random int from 0 to 9?

Randomness vs. Determinism Are there random numbers? Output RNG Can a computer generate them? A “ black box ” model of a random number generator.

Randomness vs. Determinism Are there random numbers?Can a computer generate them? The RNG revealed. Output YesNot without help! Periodic! p =

Some random history… True randomness is valuable! but not that valuable!

A random function… print the guesses ? return the number of guesses ? from random import * def guess( hidden ): """ guesses the user's hidden # """ compguess = choice( range(100) ) if compguess == hidden: # at last! print ('I got it!’) else: guess( hidden ) This is a bit suspicious… slow down…

The final version from random import * import time def guessFinal( hidden ): """ guesses the user's hidden # """ compguess = choice( range(100) ) print ('I choose', compguess) time.sleep(0.05) if compguess == hidden: # at last! print ('I got it!’) return 0 else: return 1 + guessFinal( hidden )

The two Monte Carlos Monte Carlo casino, Monaco Making random numbers work for you! Monte Carlo methods, Math/CS

Monte Carlo in action Suppose you roll two dice. What are the chances that you roll doubles? def countDoubles( N ): """ inputs a # of dice rolls outputs the # of doubles """ if N == 0: return 0 # zero rolls, zero doubles… else: d1 = choice( [1,2,3,4,5,6] ) d2 = choice( range(1,7) ) if d1 != d2: return countDoubles( N-1 ) # not doubles else: return # doubles! one roll of the dice where is the doubles check? input is the total number of rolls what should the last line be?

Monty Hall Let ’ s make a deal ’ 63- ’ 86 inspiring the “ Monty Hall paradox ”

Monte Carlo Monty Hall Suppose you always switch to the other door... What are the chances that you will win the car ? Run it (randomly) 1000 times and see!

Monte Carlo Monty Hall def MCMH( init, sors, N ): """ plays the same "Let's make a deal" game, N times returns the number of times you win the car """ if N == 0: return 0 # don't play, can't win carDoor = choice([1,2,3]) # where is the car? if init == carDoor and sors == 'stay': result = 'Car!' elif init == carDoor and sors == 'switch': result = 'Spam.' elif init != carDoor and sors == 'switch': result = 'Car!' else: result = 'Spam.' print ('You get the', result) if result == 'Car!': return 1 + MCMH( init, sors, N-1 ) else: return 0 + MCMH( init, sors, N-1 ) Your initial choice! 'switch' or 'stay' number of times to play