Hack #1 - Class Notes

Write any extra notes you have here 1) We run simulations all the time to test projected results in real life 2) Simulations could help us represent various real life scenarios with many of their random values and nature with spinners and rngs 3) Many different examples can be simulated 1) Air traffic 2) Chemical Reactions 3) Missile launches 4) We can use the python random module to run various simulations in our programs. For instance, random number generators could be used to simulate different probabilities and such.

Hack #2 - Functions Classwork

from random import *
lo = int(input("What is your desired lower range?"))
hi = int(input("What is your desired upper range?"))
x = randint(lo,hi)
print("Your random number generated on the interval of {0} to {1} was {2}.".format(lo, hi, x))

closet = ["Shoes", "T-Shirts", "pants", "boxers", "socks", "jacket"]
new_closet = ["Hoodie", "boots", "umbrella"]
def randomTrash():
    x = choice(closet)
    closet.remove(x)
    print("Removed random piece of clothing '{0}' from closet".format(x))

def randomAdd():
    y = choice(new_closet)
    closet.append(y)
    new_closet.remove(y)
    print("Moved a random piece of clothing '{}' from another closet to the main closet".format(y))

def coinflip():         
    if randint(1, 3) > 1:
        print("Heads")
    else:
        print("Tails")

randomTrash()
randomAdd()
print(closet)



#Tossing the coin 5 times:
coinflip()
coinflip()
coinflip()
coinflip()
coinflip()
Your random number generated on the interval of 10 to 100 was 92.
Removed random piece of clothing 'T-Shirts' from closet
Moved a random piece of clothing 'Hoodie' from another closet to the main closet
['Shoes', 'pants', 'boxers', 'socks', 'jacket', 'Hoodie']
Heads
Heads
Heads
Tails
Heads

coinflip example

Hack #3 - Binary Simulation Problem

from random import randint

def randomnum(): # function for generating random int
    return randint(0,255)

def converttobin(n): # function for converting decimal to binary
    x = n
    N = 7
    res = []
    while N >= 0:
        if (x - 2 ** N) >= 0:
            res.append(1)
            x-=2 ** N
            N-=1
        else:
            res.append(0)
            N-=1
    return res


def survivors(): # function to assign position
    survivorstatus = ["Jiya", "Shruthi", "Noor", "Ananya" , "Peter Parker", "Andrew Garfield", "Tom Holland", "Tobey Maguire"]
    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("\t - {}".format(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: 
	 - Jiya
	 - Shruthi
	 - Ananya
	 - Tom Holland
for i in range(8):
    print(i)
0
1
2
3
4
5
6
7

Hack #4 - Thinking through a problem

  • create your own simulation involving a dice roll
  • should include randomization and a function for rolling + multiple trials
def snakeeyes():
    x = randint(1,6)
    y = randint(1,6)
    print("Dice1: ", x)
    print("Dice2: ", y)
    if x == y == 1:
        print("Snake Eyes!")
        return
    print("Nothing Special :(")

def runtrials():
    trials = 10
    counter = 0
    while counter < trials:
        print("Trial {0}".format(counter+1))
        snakeeyes()
        counter+=1

runtrials()
Trial 1
Dice1:  2
Dice2:  3
Nothing Special :(
Trial 2
Dice1:  5
Dice2:  4
Nothing Special :(
Trial 3
Dice1:  1
Dice2:  1
Snake Eyes!
Trial 4
Dice1:  3
Dice2:  1
Nothing Special :(
Trial 5
Dice1:  3
Dice2:  4
Nothing Special :(
Trial 6
Dice1:  3
Dice2:  3
Nothing Special :(
Trial 7
Dice1:  2
Dice2:  3
Nothing Special :(
Trial 8
Dice1:  3
Dice2:  5
Nothing Special :(
Trial 9
Dice1:  1
Dice2:  5
Nothing Special :(
Trial 10
Dice1:  1
Dice2:  6
Nothing Special :(

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
from random import shuffle
questions = [
    ("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 simulation may accidentally contain bias due to the exclusion of details.", 
    "C) If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation.", 
    "D) The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output.",
    ["D"]),
    ("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) random", 
    "B) randint", 
    "C) turtle", 
    "D) tensorflow",
    ["A"]),
    ("Which of the following ways can we best simulate a spinner?",
    "A) Having the user randomly pick a number.", 
    "B) Using the randint method from the random package over a range of values to generate a value.", 
    "C) Hard code a random sequence in the source code.", 
    "D) Use some pattern in nature to return a random value",
    ["B"])
]

correct = 0
total = 6
shuffle(questions)
def printQuestion(tup):
    print(tup[0])
    print(tup[1])
    print(tup[2])
    print(tup[3])
    print(tup[4])

def questionloop():
    correct = 0
    for i in range(len(questions)):
        printQuestion(questions[i])
        ans = input("What is your answer? ")
        if ans in questions[i][5]:
            print("Correct! Nice Job!")
            correct+=1
        else:
            print("Try again next time")
    return correct

correct = questionloop()
print("You got a total of {0} out of {1} problems right!".format(correct, total))
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.
Correct! Nice Job!
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.
Try again next time
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 simulation may accidentally contain bias due to the exclusion of details.
C) If the simulation is found to contain bias, then it is not possible to remove the bias from the simulation.
D) The only way for the simulation to be biased is if the researcher intentionally used data that favored their desired output.
Correct! Nice Job!
Which of the following ways can we best simulate a spinner?
A) Having the user randomly pick a number.
B) Using the randint method from the random package over a range of values to generate a value.
C) Hard code a random sequence in the source code.
D) Use some pattern in nature to return a random value
Correct! Nice Job!
What module in python could be used to generate random values in simulation experiments?
A) random
B) randint
C) turtle
D) tensorflow
Correct! Nice Job!
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.
Try again next time
You got a total of 4 out of 6 problems 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...

import math, random
def pendulum(length, angle):
    h = length - length*math.sin(angle)
    v = math.sqrt(2*9.8 * h)
    return v

for i in range(5):
    l = (random.randint(1,10)/10)
    theta = random.randint(0,179)
    print("The speed of a pendulum with length {0} and angle {1} is around {2} m/s".format(l, theta, pendulum(l, theta)))
The speed of a pendulum with length 0.2 and angle 84 is around 1.0226895644880192 m/s
The speed of a pendulum with length 0.9 and angle 70 is around 1.9971400495004945 m/s
The speed of a pendulum with length 0.8 and angle 52 is around 0.4579075854418543 m/s
The speed of a pendulum with length 0.5 and angle 143 is around 4.4253569953806435 m/s
The speed of a pendulum with length 0.2 and angle 70 is around 0.9414608479873579 m/s