Lesson 3.16
Jupyter Notebook for the rest of the class to follow along with.
- 3.16 Intro to Simulations - Alexa
- Guiding questions for a simulation:
- Examples of Simulations:
- Simulation vs. Experiment
- A Real Life Example: Four Corners - Lydia & Ava
- Rolling a Dice Example - Sri
- Game of Life - Lydia & Ava
- Hacks Overview
Simulation vs. Experiment
- Experiment definition: procedure undertaken to make a discovery, test a hypothesis, or demonstrate a known fact
So, why use a simulation?
-
Advantages:
- Can be safer
- More cost-effective
- More efficient
- More data in less time
-
Disadvantages:
- Not as accurate as experiments
- outside factors not included (ex: in rolling dice simulation gravity and air resistance)
- When do you not use a simulation?
- when a situation already has set results/data (won't change)
- examples: a score in a game, most purchased food, average yearly wage
leads into real life game example:
A Real Life Example: Four Corners - Lydia & Ava
- an example of a simulation and experiment = the game of 4 corners
- games are simulations!
- We are going to play a round of 4 corners here in class.
- This game is a real life version of our simulation.
- Mr. Mortensen will be the person in the middle choosing corners.
- Everyone will choose a corner, and if your corner is chosen, you are out!
The rules of the game:
- a person stands in the center of a room and the 4 coners are labeled 1-4
- Every player chooses a corner while the person in the middle closes their eyes
- person in the middle chooses/calls out a number when instructed
- every player in the chosen corner is now out
- contine until there is a winner!
Let's play!
import random
status = "in"
while status != "out":
chooseCorner = input("What corner do you choose?")
corner = random.randint(1,4)
if int(chooseCorner) == corner:
status = "out"
print("You chose corner number " + chooseCorner + " and you're OUT")
else:
print("You chose corner number " + chooseCorner + " and are still in!")
Comparing Experiment to Simulation Code:
- Mr. Mortensen (person in the middle) = random.randint
- if statement = if player is out
- else = safe/continue
- while loop = repeats process until there is a winner of the game
DEBRIEF QUESTIONS:
- Why is it better to code simulations than experiement in real life?
- What makes this game a simulation?
- What are its advantages and disadvantages?
- Would an experiment be better in this situation? (raise hands for each team)
questions_number = 6
answers_correct = 0
questions = [
"True or False: Simulations will always have the same result. \n A: True, \n B: False",
"True or False: A simulation has results that are more accurate than an experiment \n A: True, \n B: False",
"True or False: A simulation can model real world events that are not practical for experiments \n A: True, \n B: False",
"Which one of these is FALSE regarding simulations \n A: Reduces Costs, \n B: Is safer than real life experiments, \n C: More Efficient, \n D: More accurate than real life experiments",
"Which of the following scenarios would be the LEAST beneficial to have as a simulation \n A: A retail company wants to identify the item which sold the most on their website, \n B: A restaurant wants to determine if the use of robots will increase efficiency, \n C: An insurance company wants to study the impact of rain on car accidents, \n D: A sports car company wants to study design changes to their new bike design ",
"Which of the following is better to do as a simulation than as a calculation \n A: Keeping score at a basketball game, \n B: Keeping track of how many games a person has won, \n C: Determining the average grade for a group of tests, \n D: Studying the impact of carbon emissions on the environment"
]
question_answers = [
"B",
"B",
"A",
"D",
"A",
"D"
]
print("Welcome to the Simulations Quiz!")
def ask_question (question, answer):
print("\n", question)
user_answer = input(question)
print("You said: ", user_answer)
if user_answer == answer:
print("Correct!")
global answers_correct
answers_correct = answers_correct + 1
else:
print("You are incorrect")
for num in range(questions_number):
ask_question(questions[num], question_answers[num])
print("You scored: ", answers_correct, "/6")
Below is a simulation of rolling dice! Examine the code and think about how this simulation works and its purpose!
def parse_input(input_string):
if input_string.strip() in {"1", "2", "3","4", "5", "6"}:
return int(input_string)
else:
print("Please enter a number from 1 to 6.")
raise SystemExit(1)
import random
def roll_dice(num_dice):
roll_results = []
for _ in range(num_dice):
roll = random.randint(1, x) # increased range of random number chosen
roll_results.append(roll)
return roll_results
num_dice_input = input("How many dice do you want to roll? [1-6] ")
x = int(input("Please enter the number of sides you want the dice to have.")) # variable for sides the dice should have
num_dice = parse_input(num_dice_input)
roll_results = roll_dice(num_dice)
print("you rolled:", roll_results)
Game of Life - Lydia & Ava
Below is a simulation of the Game of Life, originally written by John Horton Conway. Mr. Mortensen has this game on the APCSP site and we think that it is a great example of an interactive simulation.
What it is
- This game is an unpredictable cellular automaton
- automaton = simulates and imitates human life, hence why this is called the game of life
- After creating the initial configuration, the game evolves without pattern
How it works
- Cells in this game are alive or dead, similar to binary where they are on or off
- The user created an initial configuration of cells on the grid, and presses play (tap the squares on the grid)
- a cells's status (alive or dead, on or off) depends on the surrounding 8 cells status (surrounding 8 boxes). Here are the rules:
- The birth rule= a dead cell (blue box) that is surrounded by at least 3 alive cells (yellow boxes), will become alive
- The death rule= an alive cell (yellow) with no or only one surviving cell around it dies (becomes blue)
- Cell survival= an alive cell (yellow) with 2 or 3 alive neighboring cells will stay alive
Try it Out!
Use the grid below to create cell figurations, press play, and watch your cells die, live, and move around!
x = input("What is the length of path 1?")
print(str(x) + " ft")
y = input("What is the length of path 2?")
print(str(y) + " ft")
z = input("What is the length of path 3?")
print(str(z) + " ft")
path1 = x
path2 = y
path3 = z
pathList = [path1, path2, path3]
fastest = min(pathList)
print("The shortest path is " + str(fastest) + " feet long.")
Use these guiding questions for a simulation:
- What makes it a simulation?
- What are it’s advantages and disadvantages?
- In your opinion, would an experiment be better in this situation?
Hack #1 (0.3)
- Create an idea for a simulation and describe it (you don’t actually have to code it just think about/answer the guiding questions).
Hack #2 (0.1)
- Simulations Quiz (either screenshot or paste quiz in your notebook)
Hack #3 (0.2)
- Describe the rolling dice simulation (answer guiding questions)
Hack #4 (0.3)
- Add a feature onto the rolling dice simulation above
- ex: a 14-sided dice or expand the purpose of the simulation (hint: use conditionals to make dice part of a game/real life situation)
Extra Credit (0.1)
- For the extra 0.1: try coding a simple simulation and describe it (guiding question)