The Research Experience for Teachers Program /
http://www.cs.appstate.edu/ret
Activity Title: “Understanding Geometric Probability by creating Monte Carlo Simulations in Scratch”
Materials List: Scratch software online or (free downloaded from MIT), student lab sheet, computer file of Monte Carlo Backgrounds.
Introduction/Motivation: The Monte Carlo method is a problem solving technique that completes a large volume of trials using random variables to compute the probability of an outcome in a simulation. A simple Monte Carlo simulation would be to flip a coin 1000 times. This method can also be used to solve more complex problems such as estimating pi using a square and an inscribed circle.
Procedure:
Background: A secret to programming is logically thinking through the process. One way to do this is to examine someone else’s code. In Scratch you can click on the green flag to run the program completely or you can click on a particular section to see what happens. Some sections of code are dependent other sections and will not appear to perform when clicked independently. Another way to help you think out your programming is to write down a procedure plan using familiar terms (sometimes called Pseudo code) that indicate the tasks that need to happen.
Lab Activity:
Part One: The relationship of areas between a square and its inscribed circle.
1. Examine the diagram below and answer the questions that follow:
2. Now find the ratio using a different value for the radius:
a) Radius = 5 : c) Radius = 12:
b) Radius = 8 : d) Radius = n:
3. What did you find in common about the ratio?
Part Two: Examining a Monte Carlo Simulation written with Scratch:
1. On your computer - Go to: https://scratch.mit.edu/projects/16816029/#editor
2. Click the green flag to start the simulation.
3. You may notice it is taking a long time. We can help speed it up. Click the red stop sign to stop the simulation.
4. Click the button that says “see inside”. Now click on Sprite 2 to examine the code. The repeat is set to 1000; change it to 500 for now. Also part way down you will see a wait command. This means for each throw it is pausing for ¼ second. That doesn’t seem long but do that a thousand times and it adds up. Change the wait to something smaller, maybe 0.05.
5. Now before you restart the simulation let’s take a look at the other two Sprites:
Describe Sprite 1’s function:
Describe Sprite 3’s function:
6. Click back onto Sprite 2 and run the simulation with the code open. Notice that parts of the code light up as they are accessed. Answer the following questions:
a
a) At the beginning you notice 3 variables set to = 0,
Throws, hit, and Pi. What is a Hit recording?
b) A Monte Carlo simulation uses random trials; the code says pick random numbers between -100 and 100 for x and y. Explain this piece of code and what these boundaries mean.
c) Towards the bottom of the code you can see a new variable ‘Distance’ introduced.
What does this variable find? Why is this important for this simulation?
d) Below this you can see that for every hit the value of pi is changed using an equation. Rewrite this equation in terms of the area of the circle and square, based on what you learned in Part One.
Part Three: Writing a Monte Carlo Simulation:
- In Part Two you learned that the Monte Carlo method could be used to estimate Pi based on the ratio relationship between the area of a square and its inscribed circle. This is possible because the if we can calculate the area of a shape (the circle) and the area of the sample space (the square around it), we know the theoretical probability that a random throw will fall within the circle: Probablity = area of circlearea of square
- Taking this a step further, the area of an unknown shape can be calculated based on the experimental probability. In order to do this you need the area of the sample space. This is a known shape around the unknown shape. It might already exist or you can create it in your simulation.
- Open a new Scratch File. In Scratch at the bottom left hand corner upload a new background by clicking on the file folder. Use the folder of backgrounds provided in class.
Your task is to create a Scratch simulation to find the area of an unknown shape(s) on the background.
- You will use the provided Monte Carlo Simulation Worksheet to record your plan of action and answers. Good Luck.
HINT: since you don’t have number parameters for your unknown shape, you need to use color parameters. Scratch Sprites can sense what color they have landed on.
Monte Carlo Simulation Worksheet
Background: RED DESIGN
Challenge: ESTIMATE THE AREA OF THE BLUE SECTION
Describe the goal of your simulation:
Coding Plan:
Run your code at least three times and record your results:
Trial 1: ______Trial 2: ______Trial 3: ______
AVERAGE AREA: ______
Monte Carlo Simulation Worksheet
Background: US FLAG
Challenge: ESTIMATE THE PERCENTAGE OF THE U.S. FLAG THAT IS RED?
Describe the goal of your simulation:
Coding Plan:
Run your code at least three times and record your results:
Trial 1: ______Trial 2: ______Trial 3: ______
AVERAGE AREA :______
Monte Carlo Simulation Worksheet
Background: IRREGULAR BULLSEYE
Challenge: ESTIMATE THE AREA OF THE WHITE SHAPE INSIDE THE BULLSEYE?
Describe the goal of your simulation:
Coding Plan:
Run your code at least three times and record your results:
Trial 1: ______Trial 2: ______Trial 3: ______
AVERAGE AREA: ______
Monte Carlo Simulation Worksheet
Background: CONTINENTAL U.S.
Challenge: ESTIMATE THE AREA OF THE CONTINENTAL US IN SQUARE MILES
(You will need to look up the farthest points N, S, E, W, and calculate the area of
the rectangle created around the map)
Describe the goal of your simulation:
Coding Plan:
Run your code at least three times and record your results:
Trial 1: ______Trial 2: ______Trial 3: ______
AVERAGE AREA : ______
Monte Carlo Simulation Worksheet
Background: GREAT LAKES
Challenge: ESTIMATE THE AREA OF THE GREAT LAKES
*(You can use tools in Google maps to find boundaries to create a rectangle)
Describe the goal of your simulation:
Coding Plan:
Run your code at least three times and record your results:
Trial 1: ______Trial 2: ______Trial 3: ______
AVERAGE AREA : ______
Monte Carlo Simulation Worksheet
Background:
Challenge:
Describe the goal of your simulation:
Coding Plan:
Run your code at least three times and record your results:
Trial 1: ______Trial 2: ______Trial 3: ______
AVERAGE AREA: ______