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

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

Embed Size (px)

Citation preview

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

CS 121 Today

Fractals and Turtles!

The Koch Curve

how random…

Page 2: 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)

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

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

Page 4: 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 """ print ("p is", p, "; b is", b) if p == 0: return 1 else: return b*power(b, p)

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

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))

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

sum, range

def sum(L):

""" input: a list of numbers, L

output: L's sum

"""

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

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 Caseif 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…

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

sum, range

def range(low,hi):

""" input: two ints, low and hi

output: int list from low up to hi

"""

excluding hi

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

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

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

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

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

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?

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

Randomness vs. Determinism

Are there random numbers?

Output

RNG

Can a computer generate them?

A “black box” model of a random number generator.

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

Randomness vs. Determinism

Are there random numbers?

Can a computer generate them?

The RNG revealed.

Output

Yes Not without help!

http://en.wikipedia.org/wiki/Mersenne_twister

Periodic!

p = 219937-1

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

Some random history…

True randomness is valuable!

http://www.rand.org/pubs/monograph_reports/MR1418/index.html

but not that valuable!

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

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…

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

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 )

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

The two Monte Carlos

Monte Carlo casino, Monaco

Making random numbers work

for you!

Monte Carlo methods, Math/CS

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

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?

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

Monty Hall

Let’s make a deal ’63-’86

inspiring the “Monty Hall paradox”

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

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!

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

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

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

Python's Etch-a-Sketch

A new human-computer interface?

from turtle import *

reset()

left(90)

forward(50)

right(90)

backward(50)

down() or up()

color('green')

tracer(1) or tracer(0)

width(5)

done()

and lots more!

degrees!

states if the pen draws or not

states if the pen animates

or not

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

Recursive Graphics

def tri(): """ draws a polygon """ forward(100) left(120) forward(100) left(120) forward(100) left(120)

there is no tri … Could we tri this with recursion?(1)

Could we create any regular n-gon?(2)

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

def chai(size): """ mystery! """ forward(size) left(90) forward(size/2.0) right(90) right(90) forward(size) left(90) left(90) forward(size/2.0) right(90) backward(size)

What does chai draw?(1)

How could you add more to each end?

Why are there two identical commands in a row?

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

Spiral

100

90

81

72.9

spiral( initLength, angle, multiplier )

close-up of innermost part of the spiral…

spiral( 100, 90, 0.9 )

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

svTree

svTree( trunkLength, levels )

svTree( 100, 4 )

and more! (if you want)

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

Help! My turtle window froze!

Your turtle window becomes unresponsive after your programruns. Type:

>>> done()

to unlock it (but then you have to close it)

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

The Koch curve

snowflake( 100, 0 )snowflake( 100, 1 ) snowflake( 100, 2 )

snowflake( 100, 3 ) snowflake( 100, 4 ) snowflake( 100, 5 )

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

Have fun!

fill(1) color("blue")