Hack #1 - Class Notes

Simulations are abstractions that mimic real world occurrences

It allows for you to simulate things that wouldn't have been able to simulated in real life

Simulations can often contain some bias, due to lack of some details

Usage of randomness in place of random outcomes

Hack #2 - Functions Classwork

import random

my_clothes = []
possible_clothes = ["Red shirt", "Orange shirt", "Yellow shirt", "Green shirt", "Blue shirt", "Purple shirt", "Red Pants", "Yellow Pants", "Blue Pants", "Orange Scarf", "Green Scarf", "Purple Scarf"]
cont = "y"

def clothes():
    user_input = input("Do you want to add or remove clothes from your closet?")
    if user_input.lower() == "add":
        choice = random.choice(possible_clothes)
        my_clothes.append(choice)
    elif user_input.lower() == "remove":
        choice = random.choice(my_clothes)
        my_clothes.remove(choice)

while cont.lower() == "y":
    clothes()
    print(my_clothes)
    cont = input("Do you want to continue? [Y/N]")
    cont = cont.lower()
    
['Blue shirt']
['Blue shirt', 'Purple shirt']
['Blue shirt', 'Purple shirt', 'Blue Pants']
['Blue shirt', 'Purple shirt', 'Blue Pants', 'Blue Pants']
['Blue shirt', 'Purple shirt', 'Blue Pants', 'Blue Pants', 'Yellow Pants']
['Blue shirt', 'Purple shirt', 'Blue Pants', 'Blue Pants', 'Yellow Pants', 'Purple shirt']
['Blue shirt', 'Purple shirt', 'Blue Pants', 'Yellow Pants', 'Purple shirt']
['Blue shirt', 'Blue Pants', 'Yellow Pants', 'Purple shirt']
['Blue shirt', 'Yellow Pants', 'Purple shirt']
['Yellow Pants', 'Purple shirt']

Hack #3 - Binary Simulation Problem

import random

def randomnum(): # function for generating random int
    number = random.randint(128,255)
    return number

def converttobin(n): # function for converting decimal to binary
    binary = bin(n)
    binary = binary[2:]
    binary = list(binary)
    return binary

def survivors(binary): # function to assign position
    survivors = ["Jiya", "Shruthi", "Noor", "Ananya" , "Peter Parker", "Andrew Garfield", "Tom Holland", "Tobey Maguire"]
    status = []
    for person in survivors:
        died = random.choice(binary)
        status.append(died)
        if died == '1':
            print(f"{person} has survived!")
        if died == '0':
            print(f"{person} has died.")
    # replace the names above with your choice of people in the house

number = randomnum()
binary = converttobin(number)
survivors(binary)
Jiya has survived!
Shruthi has survived!
Noor has died.
Ananya has survived!
Peter Parker has survived!
Andrew Garfield has died.
Tom Holland has died.
Tobey Maguire has died.

Hack #4 - Thinking through a problem

  • create your own simulation involving a dice roll
  • should include randomization and a function for rolling + multiple trials
import random


def roll(times):
    print(f"Rolling the dice {times} times... ")
    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    six = 0
    i=0
    while i <= times:
        result = random.randint(1,6)
        if result == 1:
            one += 1
        elif result == 2:
            two += 1
        elif result == 3:
            three += 1
        elif result == 4:
            four += 1
        elif result == 5:
            five += 1
        elif result == 6:
            six += 1
        print(f"Rolling the dice... {result}!")
        i+=1
    return one, two, three, four, five, six

times = input("How many times do you want to roll the dice?")
one, two, three, four, five, six = roll(int(times))
total = one+two+three+four+five+six
print(f"""
Occurrences:
1: {one} ({round(((one/total)*100), 2)}%)
2: {two} ({round(((two/total)*100), 2)}%)
3: {three} ({round(((three/total)*100), 2)}%)
4: {four} ({round(((four/total)*100), 2)}%)
5: {five} ({round(((five/total)*100), 2)}%)
6: {six} ({round(((six/total)*100), 2)}%)
""")
Rolling the dice 100 times... 
Rolling the dice... 5!
Rolling the dice... 4!
Rolling the dice... 4!
Rolling the dice... 2!
Rolling the dice... 2!
Rolling the dice... 3!
Rolling the dice... 6!
Rolling the dice... 4!
Rolling the dice... 2!
Rolling the dice... 3!
Rolling the dice... 2!
Rolling the dice... 6!
Rolling the dice... 4!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 6!
Rolling the dice... 3!
Rolling the dice... 4!
Rolling the dice... 2!
Rolling the dice... 1!
Rolling the dice... 6!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 1!
Rolling the dice... 3!
Rolling the dice... 6!
Rolling the dice... 6!
Rolling the dice... 3!
Rolling the dice... 6!
Rolling the dice... 2!
Rolling the dice... 5!
Rolling the dice... 4!
Rolling the dice... 4!
Rolling the dice... 1!
Rolling the dice... 2!
Rolling the dice... 2!
Rolling the dice... 4!
Rolling the dice... 5!
Rolling the dice... 4!
Rolling the dice... 4!
Rolling the dice... 1!
Rolling the dice... 2!
Rolling the dice... 1!
Rolling the dice... 4!
Rolling the dice... 5!
Rolling the dice... 3!
Rolling the dice... 1!
Rolling the dice... 5!
Rolling the dice... 4!
Rolling the dice... 4!
Rolling the dice... 2!
Rolling the dice... 2!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 1!
Rolling the dice... 5!
Rolling the dice... 2!
Rolling the dice... 1!
Rolling the dice... 6!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 4!
Rolling the dice... 1!
Rolling the dice... 3!
Rolling the dice... 4!
Rolling the dice... 2!
Rolling the dice... 2!
Rolling the dice... 2!
Rolling the dice... 3!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 6!
Rolling the dice... 3!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 1!
Rolling the dice... 5!
Rolling the dice... 4!
Rolling the dice... 6!
Rolling the dice... 4!
Rolling the dice... 5!
Rolling the dice... 2!
Rolling the dice... 5!
Rolling the dice... 5!
Rolling the dice... 2!
Rolling the dice... 4!
Rolling the dice... 5!
Rolling the dice... 2!
Rolling the dice... 2!
Rolling the dice... 6!
Rolling the dice... 1!
Rolling the dice... 1!
Rolling the dice... 1!
Rolling the dice... 4!
Rolling the dice... 4!
Rolling the dice... 2!
Rolling the dice... 4!
Rolling the dice... 3!
Rolling the dice... 1!
Rolling the dice... 4!
Rolling the dice... 2!

Occurrences:
1: 14 (13.86%)
2: 21 (20.79%)
3: 10 (9.9%)
4: 22 (21.78%)
5: 23 (22.77%)
6: 11 (10.89%)

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. Simulations are _
    • answer options
      1. abstractions that mimic real world occurrences
      2. abstractions that are results from real world occurrences
      3. abstractions that cannot be replicated
      4. fun
  6. What module can you use to simulate random events?
    • answer options
      1. Os
      2. Time
      3. Math
      4. Random
questions = 6
correct = 0


Q1 = input()
if Q1 == "2": 
    print("Correct!")
    correct += 1

Q2 = input()
if Q2 == "2":
    print("Correct!")
    correct += 1
   
Q3 = input()
if Q3 == "3":
    print("Correct!")
    correct += 1

Q4 = input()
if Q4 == "2":
    print("Correct!")
    correct += 1

Q5 = input()
if Q5 == "1":
    print("Correct!")
    correct += 1

Q6 = input()
if Q6 == "4":
    print("Correct!")
    correct += 1

print( " you scored " + str(correct) +"/" + str(questions))
Correct!
Correct!
Correct!
Correct!
Correct!
Correct!
 you scored 6/6

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...

def position(angle, strength, time):
    strength = 1/strength
    pos = -(strength)*(time**2)+(angle*time)
    return pos

def velocity(angle, strength, time):
    strength = 1/strength
    velo = -(2*strength)*(time)+(angle)
    return velo

def acceleration(strength):
    strength = 1/strength
    accel = -(2*strength)
    return accel

angle = input("What angle do you want to throw the ball at?")
angle = int(angle)
strength = input("How hard do you want to throw the ball?")
strength = int(strength)

time = 0
while True:
    pos = position(angle,strength,time)
    velo = velocity(angle,strength,time)
    accel = acceleration(strength)
    print(f"The ball at {time} seconds is {pos} feet off the ground, moving at {velo} feet per second, and accelerating at {accel} feet per second, per second")
    if pos == 0:
        if time == 0:
            time += 1
        elif time != 0:
            break
    else:
        time += 1
The ball at 0 seconds is 0.0 feet off the ground, moving at 1.0 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 1 seconds is 0.875 feet off the ground, moving at 0.75 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 2 seconds is 1.5 feet off the ground, moving at 0.5 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 3 seconds is 1.875 feet off the ground, moving at 0.25 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 4 seconds is 2.0 feet off the ground, moving at 0.0 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 5 seconds is 1.875 feet off the ground, moving at -0.25 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 6 seconds is 1.5 feet off the ground, moving at -0.5 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 7 seconds is 0.875 feet off the ground, moving at -0.75 feet per second, and accelerating at -0.25 feet per second, per second
The ball at 8 seconds is 0.0 feet off the ground, moving at -1.0 feet per second, and accelerating at -0.25 feet per second, per second