Unit 4

Simulation II

Conceptual Questions

[27.1] Which of the scenarios below are an example of an event based simulation? Select all that apply.

  1. The simulation begins when the “S” key is pressed.

  2. The game starts when the mouse is clicked.

  3. A square gets larger after 10 seconds have passed.

  4. A ball changes directions after hitting the left side of the canvas.

  5. A character jumps up after a swipe up on the touchpad.

  6. All of the above.

[27.2] How do we access the character a user clicks on, given an event parameter? (fill in the blank)

 event._____ 

[27.3] How do we access the “name” of a character the user clicks on, specifically for keys that are not characters (i.e., escape, space or backspace?). (fill in the blank)

event._____

[27.4] How can we access the coordinates of where the user clicks the mouse on a window?

Choose the correct option:

  1. data.cx, data.cy

  2. event.x, event.y

  3. data.x, data.y

  4. event.cx, event.cy

[27.5] What is the law of large numbers? (fill in the blanks)

The average of the results will approach the _____ of the true answer as the number of trials gets _____. 

Code Reading

[27.6] Let’s say we have an existing function to run an individual trial called runTrial() which returns True (if the trial was successful) or False otherwise. You are trying to calculate the expected value of when the function is successful (namely, returns True). What’s wrong with the following Monte Carlo method implementation?
Hint: there are 3 errors to catch!

def getExpectedValue(numTrials):
    for trial in range(numTrials):
        count = 0
        result = runTrial() # run a new simulation
        if result == False: # check the result
            count = count + 1
    return count

Code Writing: Experiments & Trials

[27.7] Write a function chooseRandom(colors) that simulates choosing a random color from the given list.

[27.8] Write a function newRainbow(colors), which simulates taking a list of colors as a parameter and returning a random shuffle of the list. Do not use random.shuffle to solve this problem, make sure your solution is non-destructive!

[27.9] Write a function flipCoin() to simulate flipping a fair coin. The function should return randomly heads or tails.

[27.10] Write a function flips5() that simulates 5 coin flips and returns how many times the coin ends up being "head". Hint: You can use the function above to help your solution!

[27.11] Using your flips5() function from the previous question, write a function getExpectedValue(numTrials) that caluclates the average number of heads per trial.

Then, call the function with increasing values of trials (e.g., 10, 100, 1000, 10000) and observe how the result changes. What value does the average converge to?

Code Writing: Simulation II

[27.12] We are developing an event-based simulation driven by keyboard input. Please implement the keyPressed function so that it displays each character typed by the user and it stops once the Enter key is pressed.

    
    def makeModel(data):
        data["message"] = ""
        data["stopped"] = False

    def makeView(data, canvas):
        canvas.create_text(200,200,text=data["message"])

    def keyPressed(data, event):
        # your code here
    
    

[27.13] Extend the previous program by implementing the mousePressed function. The program should randomly update the text color each time the user clicks the mouse. It does not matter where the user clicks.

[27.14] Using the keyPressed function, develop a program that renders shapes based on user input. Pressing 's' should display a square, while 'c' should display a circle. Additionally, allow the user to toggle the shape's color between red, green, and blue by pressing the 'r', 'g', and 'b' keys, respectively.

    
    def makeModel(data):
        data["cx"] = 200
        data["cy"] = 200
        data["shape"] = "square"
        data["color"] = "black"
        
    def makeView(data,canvas):
        if data["shape"] == "square":
            canvas.create_rectangle(data["cx"]-50,data["cy"]-50,data["cx"]+50,data["cy"]+50,fill=data["color"])
        elif data["shape"] == "circle":
            canvas.create_oval(data["cx"]-50,data["cy"]-50,data["cx"]+50,data["cy"]+50,fill=data["color"])
    
    

[27.15] Extend the previous program by implementing the mousePressed function so that the shape moves to wherever the user clicks on the canvas.