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 π