Vocab

  • Simulation: a tested scenario used for viewing results/outputs to prepare for them in real world situations

Simulations

  • Simulation
    • a tested scenario used for viewing results/outputs to prepare for them in real world situations
    • Can be used for games like dice rolling, spinners, etc
    • Can be used for practical things such as building structures, testing car crashes, and other things before engaging in them in the real world
    • Can have the option of obeying real world physics (Gravity, collision) or they can go against these norms since this is a fictitious scenario, and couldn't happen in real life
  • Big Questions
    • Which of the following simulations could be the LEAST useful?
      • A retailer trying to identify which products sold the most (correct!)
      • A restaurant determining the efficiency of robots
      • An insurance company studying the rain impact of cars
      • A sports bike company studying design changes to their new bike design
    • If you were making a simulation for making a new train station, which of the following would be true about this simulation?
      • It could reveal potential problems/safety issues before construction starts (correct!)
      • It cannot be used to test the train station in different weather
      • Simulation will add high costs to projects
      • Simulation is not needed because this train station already exists
  • Simulation 1
    • Both programs below do the same thing. Given a height and a weight, they calculate how long it will take for a object to fall to the ground in a vacuum subjected to normal Earth levels of gravity.
    • However, the second one is a simulation. It calculates the distance the object has fallen every 0.1 seconds. This is useful for if you wanted a visual representation of a falling object, which pure math can't do as smoothly.
height = float(input("height in meters?"))

weight = input("weight in pounds?")

stuff = (2 * (height / 9.8))**(1/2)

print("It will take", stuff,"seconds for an object that weighs",weight,"pounds","to fall ",height,"meters in a vacuum")
It will take 1.2777531299998797 seconds for an object that weighs 9 pounds to fall  8.0 meters in a vacuum
t = 0
g = 0
d = 0
false = True
while false:
    t = t + 0.1
    d = 9.8 / 2 * (t**2)
    if d >= height:
        false = False
    #print(d) # if you want to print the distance every time it calculates it. Too long to output to a terminal, but this could be useful to display graphically. 
    #print(t)

print(t)
print(d)
1.3
8.281
  • Simulation 2
    • This simulation is made in order to simulate movement on a 2d plane vs a 3d plane.
    • How it works: we have multiple variables, if statements and equations under a while command in order to random generate steps on a 2d plane. Once it reaches the set destination, it will say that the man made it home after x amount of steps.
    • For the 3D plane, it takes a lot longer due to how big and open the 3d environment is, so there are more if statements in the 3d plane
import random
x = 0
y = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    step = random.randrange(4)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1

    turn = turn + 1

    if x == 0 and y == 0:
        nights = nights + 1
        print("The Man Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y != 0:
        print("(", x,y, ")")
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Average", average, "Ones that when't too long ", stopped)
import random
x = 0
y = 0
z = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    #random movement
    step = random.randrange(6)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1
    if step == 4:
        z = z+1
    if step == 5:
        z = z-1
    #Turn counter
    turn = turn + 1
    #Goal check
    if x == 0 and y == 0 and z == 0:
        nights = nights + 1
        print("The Bird Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y + z != 0:
        print("(", x,y, ") ","| ", z)
    #Too long Stopper
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        z = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Average", average,"Ones that when't too long ", stopped)

HW

  • Create a simulation. It can be anything, just has to simulate something
import random

print("Welcome to my stock simulation. You will start with $1000. The goal is to reach $1,000,000 in the least amount of weeks. Test how good your strategies are and see how lucky you would be in real life! If you lose half of the initial investment, YOU LOSE, simulation over.")

win_rate = int(input("What is your win rate? (input integer number 1-100)"))
win_percent_floor = int(input("When you win, what is the minimum gain in percent? (input integer number)"))
win_percent_ceiling = int(input("When you win, what is the maximum gain in percent? (input integer number)"))
loss_percent_floor = int(input("When you lose, what is the minimum loss in percent? (input integer number)"))
loss_percent_ceiling = int(input("When you lose, what is the maximum loss in percent? (input integer number)"))

initial_invest = 1000
profit_target = 1000000
accumulated_profit = initial_invest
trade_count = 1

while accumulated_profit < profit_target:
   won = random.randint(1, 100) < win_rate
   if won:
        win_percent = random.randint(win_percent_floor, win_percent_ceiling)
        accumulated_profit = accumulated_profit * (1 + float(win_percent / 100))
        print("Winning trade! Accumulated profit: " + str(round(accumulated_profit, 2))+ " Trade count: " + str(trade_count))
   else:
        loss_percent = random.randint(loss_percent_floor, loss_percent_ceiling)
        accumulated_profit = accumulated_profit * (1 - float(loss_percent / 100))
        print("Losing trade! Accumulated profit: " + str(round(accumulated_profit, 2)) + " Trade count: " + str(trade_count))
        if accumulated_profit < 500:
            print("Game over, your strategy fell short of success")
            break
   trade_count = trade_count + 1
    
Welcome to my stock simulation. You will start with $1000. The goal is to reach $1,000,000 in the least amount of weeks. Test how good your strategies are! If you lose half of the initial investment, YOU LOSE, simulation over.
Winning trade! Accumulated profit: 1130.0 Trade count: 1
Winning trade! Accumulated profit: 1265.6 Trade count: 2
Winning trade! Accumulated profit: 1442.78 Trade count: 3
Losing trade! Accumulated profit: 1399.5 Trade count: 4
Losing trade! Accumulated profit: 1329.53 Trade count: 5
Winning trade! Accumulated profit: 1515.66 Trade count: 6
Winning trade! Accumulated profit: 1667.22 Trade count: 7
Winning trade! Accumulated profit: 1833.95 Trade count: 8
Winning trade! Accumulated profit: 2072.36 Trade count: 9
Winning trade! Accumulated profit: 2362.49 Trade count: 10
Winning trade! Accumulated profit: 2716.86 Trade count: 11
Winning trade! Accumulated profit: 3097.23 Trade count: 12
Losing trade! Accumulated profit: 2973.34 Trade count: 13
Winning trade! Accumulated profit: 3330.14 Trade count: 14
Winning trade! Accumulated profit: 3763.05 Trade count: 15
Losing trade! Accumulated profit: 3650.16 Trade count: 16
Winning trade! Accumulated profit: 4124.68 Trade count: 17
Winning trade! Accumulated profit: 4537.15 Trade count: 18
Losing trade! Accumulated profit: 4310.3 Trade count: 19
Winning trade! Accumulated profit: 4956.84 Trade count: 20
Winning trade! Accumulated profit: 5700.37 Trade count: 21
Losing trade! Accumulated profit: 5529.35 Trade count: 22
Winning trade! Accumulated profit: 6358.76 Trade count: 23
Winning trade! Accumulated profit: 7312.57 Trade count: 24
Winning trade! Accumulated profit: 8043.83 Trade count: 25
Losing trade! Accumulated profit: 7641.64 Trade count: 26
Winning trade! Accumulated profit: 8787.88 Trade count: 27
Winning trade! Accumulated profit: 10018.19 Trade count: 28
Winning trade! Accumulated profit: 11220.37 Trade count: 29
Losing trade! Accumulated profit: 10659.35 Trade count: 30
Winning trade! Accumulated profit: 11938.47 Trade count: 31
Winning trade! Accumulated profit: 13132.32 Trade count: 32
Winning trade! Accumulated profit: 15102.17 Trade count: 33
Winning trade! Accumulated profit: 17216.47 Trade count: 34
Losing trade! Accumulated profit: 16699.98 Trade count: 35
Winning trade! Accumulated profit: 19037.97 Trade count: 36
Losing trade! Accumulated profit: 18276.45 Trade count: 37
Winning trade! Accumulated profit: 20835.16 Trade count: 38
Losing trade! Accumulated profit: 20418.46 Trade count: 39
Winning trade! Accumulated profit: 23072.85 Trade count: 40
Losing trade! Accumulated profit: 22611.4 Trade count: 41
Winning trade! Accumulated profit: 25324.77 Trade count: 42
Winning trade! Accumulated profit: 28870.23 Trade count: 43
Losing trade! Accumulated profit: 28004.13 Trade count: 44
Losing trade! Accumulated profit: 26883.96 Trade count: 45
Winning trade! Accumulated profit: 30110.04 Trade count: 46
Winning trade! Accumulated profit: 34626.54 Trade count: 47
Winning trade! Accumulated profit: 38089.19 Trade count: 48
Winning trade! Accumulated profit: 41898.11 Trade count: 49
Winning trade! Accumulated profit: 46506.91 Trade count: 50
Winning trade! Accumulated profit: 52087.74 Trade count: 51
Losing trade! Accumulated profit: 50004.23 Trade count: 52
Winning trade! Accumulated profit: 56004.73 Trade count: 53
Losing trade! Accumulated profit: 54884.64 Trade count: 54
Losing trade! Accumulated profit: 53238.1 Trade count: 55
Winning trade! Accumulated profit: 60159.05 Trade count: 56
Losing trade! Accumulated profit: 58354.28 Trade count: 57
Winning trade! Accumulated profit: 65356.79 Trade count: 58
Winning trade! Accumulated profit: 73853.18 Trade count: 59
Winning trade! Accumulated profit: 84192.62 Trade count: 60
Losing trade! Accumulated profit: 79982.99 Trade count: 61
Winning trade! Accumulated profit: 91180.61 Trade count: 62
Losing trade! Accumulated profit: 88445.19 Trade count: 63
Losing trade! Accumulated profit: 84022.93 Trade count: 64
Winning trade! Accumulated profit: 96626.37 Trade count: 65
Winning trade! Accumulated profit: 108221.54 Trade count: 66
Winning trade! Accumulated profit: 120125.91 Trade count: 67
Losing trade! Accumulated profit: 116522.13 Trade count: 68
Winning trade! Accumulated profit: 129339.56 Trade count: 69
Losing trade! Accumulated profit: 122872.59 Trade count: 70
Winning trade! Accumulated profit: 137617.3 Trade count: 71
Losing trade! Accumulated profit: 134864.95 Trade count: 72
Winning trade! Accumulated profit: 152397.39 Trade count: 73
Winning trade! Accumulated profit: 173733.03 Trade count: 74
Winning trade! Accumulated profit: 191106.33 Trade count: 75
Losing trade! Accumulated profit: 185373.14 Trade count: 76
Winning trade! Accumulated profit: 213179.11 Trade count: 77
Winning trade! Accumulated profit: 245155.98 Trade count: 78
Losing trade! Accumulated profit: 232898.18 Trade count: 79
Losing trade! Accumulated profit: 221253.27 Trade count: 80
Winning trade! Accumulated profit: 254441.26 Trade count: 81
Winning trade! Accumulated profit: 279885.39 Trade count: 82
Winning trade! Accumulated profit: 307873.93 Trade count: 83
Winning trade! Accumulated profit: 347897.54 Trade count: 84
Winning trade! Accumulated profit: 389645.24 Trade count: 85
Winning trade! Accumulated profit: 432506.22 Trade count: 86
Winning trade! Accumulated profit: 488732.03 Trade count: 87
Losing trade! Accumulated profit: 478957.39 Trade count: 88
Winning trade! Accumulated profit: 541221.85 Trade count: 89
Losing trade! Accumulated profit: 519572.97 Trade count: 90
Losing trade! Accumulated profit: 509181.51 Trade count: 91
Winning trade! Accumulated profit: 580466.92 Trade count: 92
Winning trade! Accumulated profit: 644318.29 Trade count: 93
Losing trade! Accumulated profit: 612102.37 Trade count: 94
Winning trade! Accumulated profit: 673312.61 Trade count: 95
Losing trade! Accumulated profit: 646380.1 Trade count: 96
Winning trade! Accumulated profit: 736873.32 Trade count: 97
Winning trade! Accumulated profit: 832666.85 Trade count: 98
Winning trade! Accumulated profit: 932586.87 Trade count: 99
Winning trade! Accumulated profit: 1044497.3 Trade count: 100
  • Find an example of a simulation in a software/game you use, screenshot, and explain how it is a simulation
    • Blender

simulation