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.

Hack #2 - Functions Classwork

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))
336
closet = ["red shoes", "green pants", "tie", "belt"]

def trash():
    closet.pop(randint(0,4))
    print(closet)
trash()
['red shoes', 'tie', 'belt']
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()
Heads
Heads
Heads
Tails
Tails

Hack #3 - Binary Simulation Problem

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)
The following people survived the apocalypse: 
     - Shruthi
     - Alex
     - Mr. Yeung
from random import *
def rand():
    x = randint(0,255)
    return bin(x)

def survive(N):
    surivors = N
    return N

print(type(bin(4)))
<class 'str'>

Hack #4 - Thinking through a problem

  • create your own simulation involving a dice roll
  • should include randomization and a function for rolling + multiple trials
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()
Trial 1
Dice 1:4
Dice 2:4
DOUBLES!!
Trial 2
Dice 1:6
Dice 2:2
L
Trial 3
Dice 1:3
Dice 2:3
DOUBLES!!
Trial 4
Dice 1:5
Dice 2:3
L
Trial 5
Dice 1:5
Dice 2:5
DOUBLES!!
Trial 6
Dice 1:2
Dice 2:4
L
Trial 7
Dice 1:1
Dice 2:2
L
Trial 8
Dice 1:6
Dice 2:2
L
Trial 9
Dice 1:4
Dice 2:1
L
Trial 10
Dice 1:1
Dice 2:2
L

Hack 5 - Applying your knowledge to situation based problems

Using the questions bank below, create a quiz that presents the user a random question and calculates the user's score. You can use the template below or make your own. Making your own using a loop can give you extra points.

  1. 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:
      1. The simulation is an abstraction and therefore cannot contain any bias
      2. The simulation may accidentally contain bias due to the exclusion of details.
      3. If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation.
      4. The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output.
  2. 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
      1. No, it's not a simulation because it does not include a visualization of the results.
      2. No, it's not a simulation because it does not include all the details of his life history and the future financial environment.
      3. Yes, it's a simulation because it runs on a computer and includes both user input and computed output.
      4. Yes, it's a simulation because it is an abstraction of a real world scenario that enables the drawing of inferences.
  3. 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
      1. Realistic sound effects based on the material of the baseball bat and the velocity of the hit
      2. A depiction of an audience in the stands with lifelike behavior in response to hit accuracy
      3. Accurate accounting for the effects of wind conditions on the movement of the ball
      4. A baseball field that is textured to differentiate between the grass and the dirt
  4. 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
      1. The simulation will not contain any bias that favors one body type over another, while an experiment will be biased.
      2. The simulation can be run more safely than an actual experiment
      3. The simulation will accurately predict the parachute's safety level, while an experiment may be inaccurate due to faulty experimental design.
      4. 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
  5. YOUR OWN QUESTION; can be situational, pseudo code based, or vocab/concept based
  6. 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!")
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.
Nice, you get a bronze medal!!
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.
Nice, you get a bronze medal!!
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.
Nice, you get a bronze medal!!
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.
Nice, you get a bronze medal!!
What module in python could be used to generate random values in simulation experiments?
A) tensorflow
B) randint
C) turtle
D) random
Nice, you get a bronze medal!!
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?
L bozo
You got  5 / 6  questions right!

Hack #6 / Challenge - Taking real life problems and implementing them into code

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}')
Welcome To SimuLotto!
Enter 5 Numbers:
You got 0 correct numbers
Result: [92, 94, 99, 60, 10]