Due Date: midnight, Friday, 6 December.

This assignment focuses on design and simulation from Chapter 9.

Submit the following programs via Blackboard:

  1. Modify the stock simulation from Lab 10 to calculate and print the average ending value of money.

  2. Is it likely that a monkey at a typewriter, randomly choosing character, could write Hamlet?

    Write a simulation to answer this question. That is, write a program that will randomly generate 100,000 strings of length 5 and count the number of strings that match "to be". You may assume that your strings are lower case letters and spaces only. That is, when you generate your string, you are choosing from the 27 characters: [' ','a','b','c','d',...'y','z']

  3. A bellwether state is one that has voted with the winner over past elections. For example, both Ohio and Nevada have voted for the presidential winner in all the elections since 1980.

    Write a function to simulate the voting of the 50 states. Your function should take as input the number of elections to simulate, and then simulate the elections in the 50 states, assuming that each has 50% probability of voting for the winner.

    In your main() function in your program, you should call your function 1000 times with the input parameter values: 1, 2, 3, 4, 5, and 10, and print out the number of bellweather states you would expect for each number of elections. That is, your output should look like:

    	    Number of Elections      Number of Bellweather states
    	    1                        your calculation goes here!
    	    2                        your calculation goes here!
    	    3                        your calculation goes here!
    	    4                        your calculation goes here!
    	    5                        your calculation goes here!
    	    9                        your calculation goes here! 
    	    10                       your calculation goes here!
    	 
  4. Chapter 9, #13 (random walk) - Suppose you are doing a random walk on the blocks of a city. At each "step" you choose to walk one block (at random) either forward, backward, left or right. In n steps, how far do you expect to be from your starting point? Write a program to help answer this question.

  5. Chapter 9, #14 (graphical random walk) - Write a graphical random walk in two dimensions. In this simulation you should allow the step to be taken in any direction. You can generate a random direction an an angle off the x axis.

    angle = random() * 2 * math.pi

    The new x and y positions are then given by these formulas:

    • x = x + cos(angle)
    • y = y + sin(angle)

    The program should take the number of steps as an input. Start your walker at the center of a 100x100 grid and draw a line that traces the walk as it progresses.

For more information on using Blackboard, see the first lab.

General Notes