require './support.php'; ?>
Homework #3: Random Processes
In this third homework, we'll being to apply what we've learned in the previous sections to handle programs where the operation is not completely deterministics (like how SELIZA could response randomly). Human behavior is complex and not easily predictable. Often the best we can do is try to understand the distribution of people's responses or behaviors. For example, not everyone will be startled by a balloon popping. However, we might be interested in the percentage of people that are. Random processes are a fundamental building block for many types of cognitive models and so it is important to get a handle on how we can use the computer to simulate processes in nature that are probabilistic.
In this first exercise we'll take a look at random processes. Take a coin out of your pocket and flip it. If it lands heads up do option A, otherwise do option B
Option A: Using either the random() or the randint() function in python and a "for" loop write a program that prints out 200 random numbers between 0 and 1 (one on each line). Copy and paste the list of numbers into a text file and upload to the virtual class chat room. You are done.
Option B: The people in step 1 are using python's random() function to generate a list of 200 random numbers. You are going to try to fake it. Open up a text file and enter 200 random numbers (either a 0 or 1). It is a long list, but just quickly type it out, trying to make it seem as random as you can. You want to make them so random looking that there is not way anyone could know if it was you or the computer that generated the numbers. Copy and paste the list of numbers into a text file and upload to the virtual class chat room. You are done.
Write a short program to simulate a 6 sided die (hint: randint() should help). Write a function which takes a single argument called 'n' which represents the number of times you will "virtually" roll the die. Have your function roll your virtual die 'n' times, then output the number of times each face of the die comes out. To do this you will need to keep track of the number of times '1' comes up, the number of times '2' comes up, and so on. Your programs output should look like this:
rolldice(100) Rolled the six sided die 100 times! Outcomes: The die landed on the 1, * times The die landed on the 2, * times The die landed on the 3, * times The die landed on the 4, * times The die landed on the 5, * times The die landed on the 6, * times
Where the *'s are replaced with the values. Try running your program with the die rolled 5 times, 10 times, 100 times, 1000 times. Notice anything? Now change you program so that the print out shows the relative frequency of each outcome (i.e., out of all N times you rolled the die what percentage were 1, 2, 3, ...) Compare the effects of increasing the value of N on these "normalized" values. Bonus style points if you don't use "if" statements in your code (it is possible!).
In your last program you made a simple model of a six-sided die (even if you didn't know it!). If you know a little probability theory we wouldn't have needed to use the computer to figure out how often we expect to roll a 6 on a six sided die (1/6). However, when we want to understand more complex processes it is often have the computer simulate the process for us, record the result, and analyze them to find out what we expect to happen. In this exercise you will modify your previous code to model a more complex die-rolling process. On each trial you will roll the die once. If the side that comes up is greater than 3, then roll the die again. Otherwise stop and record the number of times you rolled the die. Your output should look like this:
keeprollingdice(100) Rolled the keep-rolling die 100 times! Outcomes: There were * outcomes where I rolled the die only 1 time There were * outcomes where I rolled the die only 2 times There were * outcomes where I rolled the die only 3 times There were * outcomes where I rolled the die only 4 times There were * outcomes where I rolled the die only 5 times ...
Where the *'s are again replaced with the values. Try changing N as you did in the previous simulations to see how things change. Again, change your program so that the print out shows the relative frequency of each outcome (i.e., out of all N times you rolled the die what percentage were 1 time, 2, 3, ...) Compare the effects of increasing the value of N on these "normalized" values. Now try changing your code so that instead of rolling the die if the face showing is greater than 3, use random() to create a number between 0.0 and 1.0. If the number is less than a variable p, then keep rolling. Change your program so that it takes to arguments, N and p, where p is the probability of rolling the die again. Again count the number of times you rolled each one.