CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 14 - Randomness


As usual, create two directories for today's class. Create a directory called lecture14 under activities, and a directory called lab14 under labs.


Randomness

Time to learn a new module! This module lets us generate random numbers, which we can then use within out programs.


In-class Activity 1

How Random(?)

Create a file called how_random.py in your lecture14 directory. Write a function that uses the turtle module, and draws a line a random distance (and location) from the origin. You should do this by generating two random integers, and going to that location on the screen.

Write another function called euclidean_distance, which takes 2 integer parameters (x and y), and returns the distance that the point is from the origin (0,0).

Using these two functions, write a for loop that draws some number of random lines radiating from the center of the turtle window. Compute and write (using turtle.write) the disance from the origin the end point of the line is.

Your for loop should also compute the average distance from the center your set of points are. Print this average to the terminal. Run your program multiple times. Does this average seem random? Does the distribution of points seem random?


More Emacs Commands


Lab Assignment 14

Estimating π

If you went to the talk last Friday, you already have a leg up on the rest of the class. Archimedes developed a mechanism that allows one to estimate π based off the radius of a circle and its circumference.

"Wait a minute!" you're probably saying to yourself. "The equation for computing the circumference of a circle relies on π!" And you would be absolutely correct. However, if we can approximate the circumference of a circle, then we could get an approximate value for π, using the following formula:

π = C / (2 × r)

Where C is the estimated circumference, and r is the radius of the circle.

In a file called estimate_pi.py in your lab14 directory, write a function that estimates π by computing the circumference of the polygon that "estimates" a circle. Your function should take the number of sides the polygon has and the radius of the circle estimated by the polygon. It should return the estimate of π based on that polygon.

Recall, from trig, that you can compute the length of a side of the polygon by using the sin of the angle. So:

arc_length = 360 / number of sides
inner angle of a right triangle = arc_length / 2

Once you have your function written, use a for loop to print the π estimate for all polygons with a number of sides s ≤ 25. Your function should choose a random radius for each. Compare your values with a team close by. Do your values vary greatly?


Testing and Debugging

Your programs are continually growing. Soon, you might get stuck simply because you left off an ")", or even a ":". One way to mitigate this is through the process of incremental design. The first step towards that is through testing your functions. Let's see how that's done.


Submission

When you have finished, create a tar file of your lab14 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czf lab14.tgz lab14/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 14. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your file.

Do not forget to email your partner todays files!


Last modified: Mon Sep 30 14:06:11 EDT 2013