Unit 3 Lesson 16, Student Copy
- Hack #1 - Class Notes
- Hack #2 - Functions Classwork
- Hack #3 - Binary Simulation Problem
- Hack #4 - Thinking through a problem
- Hack 5 - Applying your knowledge to situation based problems
- Hack #6 / Challenge - Taking real life problems and implementing them into code
Hack #1 - Class Notes
- We run simulations in order to test estimated results in real life scenarios
- Simulations help greatly with accounting for variables that would happen in real life scenarios
- Numerous real life examples can be simulated
- The random module/library is great for simulations, as it helps with the recreation of real life variables.
from random import *
userLow = int(input("What's the lowest number you would want to print?")) # inputted 1
userHigh = int(input("What's the highest number you would want to print?")) # inputted 403
print(randint(userLow, userHigh))
closet = ["red shoes", "green pants", "tie", "belt"]
def trash():
closet.pop(randint(0,4))
print(closet)
trash()
import random
def coinflip(): #def function
randomflip = random.randint(1, 3) #picks either 1, 2, or 3 randomly (33.3% chance of either)
if randomflip >= 2: #assigning 2 and 3 to be heads--> if either are chosen then it will print, "Heads"
print("Heads")
else:
if randomflip == 1: #assigning 1 to be tails--> if 1 is chosen then it will print, "Tails"
print("Tails")
#Tossing the coin 5 times:
t1 = coinflip()
t2 = coinflip()
t3 = coinflip()
t4 = coinflip()
t5 = coinflip()
from random import randint
def randomnum(): # function for generating random int
return randint(0,255)
def converttobin(dec): # function for converting decimal to binary
N = dec
i = 7
binary = []
while i >= 0:
if (N - 2 ** i) >= 0:
binary.append(1)
N-=2 ** i
i-=1
else:
binary.append(0)
i-=1
return binary
def survivors(): # function to assign position
survivorstatus = ["Jiya", "Shruthi", "Noor", "Ananya" , "Alex", "Nathan", "Haseeb", "Mr. Yeung"]
chances = converttobin(randomnum())
return [survivorstatus[i] for i in range(8) if chances[i] == 1]
def printsurvivors(survivor_list):
print("The following people survived the apocalypse: ")
for i in survivor_list:
print(" -", i)
# replace the names above with your choice of people in the house
survivor_list = survivors()
printsurvivors(survivor_list)
from random import *
def rand():
x = randint(0,255)
return bin(x)
def survive(N):
surivors = N
return N
print(type(bin(4)))
from random import randint as r
def dice():
dice1 = r(1,6)
dice2 = r(1,6)
print("Dice 1:" + str(dice1))
print("Dice 2:" + str(dice2))
if dice1 == dice2:
print("DOUBLES!!")
return
else:
print("L")
def diceRolls():
totalRolls = 10
curRolls = 0
while curRolls < totalRolls:
print("Trial {0}".format(curRolls+1))
dice()
curRolls = curRolls + 1
diceRolls()
- A researcher gathers data about the effect of Advanced Placement®︎ classes on students' success in college and career, and develops a simulation to show how a sequence of AP classes affect a hypothetical student's pathway.Several school administrators are concerned that the simulation contains bias favoring high-income students, however.
- answer options:
- The simulation is an abstraction and therefore cannot contain any bias
- The simulation may accidentally contain bias due to the exclusion of details.
- If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation.
- The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output.
- answer options:
- Jack is trying to plan his financial future using an online tool. The tool starts off by asking him to input details about his current finances and career. It then lets him choose different future scenarios, such as having children. For each scenario chosen, the tool does some calculations and outputs his projected savings at the ages of 35, 45, and 55.Would that be considered a simulation and why?
- answer options
- No, it's not a simulation because it does not include a visualization of the results.
- No, it's not a simulation because it does not include all the details of his life history and the future financial environment.
- Yes, it's a simulation because it runs on a computer and includes both user input and computed output.
- Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.
- answer options
- Sylvia is an industrial engineer working for a sporting goods company. She is developing a baseball bat that can hit balls with higher accuracy and asks their software engineering team to develop a simulation to verify the design.Which of the following details is most important to include in this simulation?
- answer options
- Realistic sound effects based on the material of the baseball bat and the velocity of the hit
- A depiction of an audience in the stands with lifelike behavior in response to hit accuracy
- Accurate accounting for the effects of wind conditions on the movement of the ball
- A baseball field that is textured to differentiate between the grass and the dirt
- answer options
- Ashlynn is an industrial engineer who is trying to design a safer parachute. She creates a computer simulation of the parachute opening at different heights and in different environmental conditions.What are advantages of running the simulation versus an actual experiment?
- answer options
- The simulation will not contain any bias that favors one body type over another, while an experiment will be biased.
- The simulation can be run more safely than an actual experiment
- The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design.
- The simulation can test the parachute design in a wide range of environmental conditions that may be difficult to reliably reproduce in an experiment.
- this question has 2 correct answers
- answer options
- YOUR OWN QUESTION; can be situational, pseudo code based, or vocab/concept based
- YOUR OWN QUESTION; can be situational, pseudo code based, or vocab/concept based
quiz = [
("A researcher gathers data about the effect of Advanced Placement®︎ classes on students' success in college and career, and develops a simulation to show how a sequence of AP classes affect a hypothetical student's pathway.Several school administrators are concerned that the simulation contains bias favoring high-income students, however.",
"A) The simulation is an abstraction and therefore cannot contain any bias.",
"B) The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output.",
"C) If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation.",
"D) The simulation may accidentally contain bias due to the exclusion of details.",
["B"]),
("Jack is trying to plan his financial future using an online tool. The tool starts off by asking him to input details about his current finances and career. It then lets him choose different future scenarios, such as having children. For each scenario chosen, the tool does some calculations and outputs his projected savings at the ages of 35, 45, and 55.Would that be considered a simulation and why?",
"A) No, it's not a simulation because it does not include a visualization of the results.",
"B) No, it's not a simulation because it does not include all the details of his life history and the future financial environment.",
"C) Yes, it's a simulation because it runs on a computer and includes both user input and computed output.",
"D) Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.",
["D"]),
("Sylvia is an industrial engineer working for a sporting goods company. She is developing a baseball bat that can hit balls with higher accuracy and asks their software engineering team to develop a simulation to verify the design.Which of the following details is most important to include in this simulation?",
"A) Realistic sound effects based on the material of the baseball bat and the velocity of the hit.",
"B) A depiction of an audience in the stands with lifelike behavior in response to hit accuracy.",
"C) Accurate accounting for the effects of wind conditions on the movement of the ball.",
"D) A baseball field that is textured to differentiate between the grass and the dirt.",
["C"]),
("Ashlynn is an industrial engineer who is trying to design a safer parachute. She creates a computer simulation of the parachute opening at different heights and in different environmental conditions. What are advantages of running the simulation versus an actual experiment?",
"A) The simulation will not contain any bias that favors one body type over another, while an experiment will be biased.",
"B) The simulation can be run more safely than an actual experiment.",
"C) The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design.",
"D) The simulation can test the parachute design in a wide range of environmental conditions that may be difficult to reliably reproduce in an experiment.",
["B", "D"]),
("What module in python could be used to generate random values in simulation experiments?",
"A) tensorflow",
"B) randint",
"C) turtle",
"D) random",
["D"]),
("True or false, simulations are terrible at emulating real world scenarios and variables",
"A) True",
"B) False",
"C) What's a simulation?",
"D) What scenarios?",
["B"])
]
correct = 0
total = 6
def printQuestions(tuple):
print(tuple[0])
print(tuple[1])
print(tuple[2])
print(tuple[3])
print(tuple[4])
def questionloop():
correct = 0
for i in range(len(quiz)):
printQuestions(quiz[i])
ans = input("What is your answer? ")
if ans in quiz[i][5]:
print("Nice, you get a bronze medal!!")
correct+=1
else:
print("L bozo")
return correct
correct = questionloop()
print("You got ", correct, "/", total, " questions right!")
Create your own simulation based on your experiences/knowledge! Be creative! Think about instances in your own life, science, puzzles that can be made into simulations
Some ideas to get your brain running: A simulation that breeds two plants and tells you phenotypes of offspring, an adventure simulation...
from random import randint as r
lucky_numbers = []
user_numbers = []
correct_numbers = 0
print('Welcome To SimuLotto!')
print('Enter 5 Numbers:')
# For generating random lucky numbers
for num in range(0,5):
random_num = r(1, 100)
lucky_numbers.append(random_num)
# For getting user numbers
for num in range(0,5):
user_num = int(input())
user_numbers.append(user_num)
# For checking if got any lucky numbers
for lucky_num in lucky_numbers:
for user_num in user_numbers:
if user_num == lucky_num:
correct_numbers = correct_numbers + 1
print(f'You got {correct_numbers} correct numbers')
print(f'Result: {lucky_numbers}')