First Order of Business: Get your notebook

  • Open a terminal in vscode, run command: cd _notebooks, type 'wget' and paste this link into said terminal and run it

  • Take notes wherever you please, but you will be graded on participating

So, what is a simulation anyway?

  • A simulation is a tested scenario used for viewing results/outputs to prepare for them in real world situations

  • These can be used for games like dice rolling, spinners, etc

  • These can be used for practical things such as building structures, testing car crashes, and other things before engaging in them in the real world

  • These simulations 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 Question

  • Which of the following simulations could be the LEAST useful?

  • A retailer trying to identify which products sold the most

  • 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 guessed a bike company, you're wrong, because the retail simulation was the right answer. Simulating robots in food service, studying rain impact on vehicles, and new bike design can contribute a lot more to society in comparison to seeing what products sell more than others.

Next Big Question

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
  • 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
  • Potential Saftey was the right answer, because you need somewhere to test the safety and ethicness of what you're about to do before you start building it. Otherwise, let's just say you'll have a special plaque for FBI's Most Wanted

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.0 seconds for an object that weighs 9999999 pounds to fall  4.9 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.0999999999999999
5.928999999999999

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

(explain further)

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("Avaerage", average, "Ones that when't too long ", stopped)
The Man Has Made It Home After  26092 Turns
The Man Has Made It Home After  16 Turns
The Man Has Made It Home After  18 Turns
The Man Has Made It Home After  55150 Turns
The Man Has Made It Home After  14 Turns
The Man Has Made It Home After  5136 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  6 Turns
( 651 -279 )
( 1218 -624 )
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/soham/vscode/csp-fastpages/_notebooks/2022-12-13-Simulations.ipynb Cell 10 in <cell line: 9>()
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/soham/vscode/csp-fastpages/_notebooks/2022-12-13-Simulations.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> turns = []
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/soham/vscode/csp-fastpages/_notebooks/2022-12-13-Simulations.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> while (nights < 100):
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/soham/vscode/csp-fastpages/_notebooks/2022-12-13-Simulations.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=9'>10</a>     step = random.randrange(4)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/soham/vscode/csp-fastpages/_notebooks/2022-12-13-Simulations.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a>     if step == 0:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/soham/vscode/csp-fastpages/_notebooks/2022-12-13-Simulations.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11'>12</a>         x = x+1

File /usr/lib/python3.8/random.py:215, in Random.randrange(self, start, stop, step, _int)
    213 if stop is None:
    214     if istart > 0:
--> 215         return self._randbelow(istart)
    216     raise ValueError("empty range for randrange()")
    218 # stop argument supplied.

File /usr/lib/python3.8/random.py:257, in Random._randbelow_with_getrandbits(self, n)
    255 r = getrandbits(k)          # 0 <= r < 2**k
    256 while r >= n:
--> 257     r = getrandbits(k)
    258 return r

KeyboardInterrupt: 
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 Stoper
    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("Avaerage", average,"Ones that when't too long ", stopped)
( -637 -442 )  |  583
( 424 -434 )  |  818
( -420 628 )  |  650
( 120 1197 )  |  1415
( 170 1667 )  |  1121
( 853 2449 )  |  582
( 589 3205 )  |  466
( 1045 3870 )  |  223
( 540 4370 )  |  1116
( 116 4888 )  |  1338
Caped
The Bird Has Made It Home After  4 Turns
The Bird Has Made It Home After  6 Turns
The Bird Has Made It Home After  2 Turns
The Bird Has Made It Home After  2 Turns
( -118 -992 )  |  -812
( -248 -989 )  |  -531
( -706 -2230 )  |  204
( 52 -2707 )  |  641
( 40 -2787 )  |  25
( 523 -2173 )  |  -1206
( 872 -1278 )  |  -1832
( 434 -1119 )  |  -1873
( -177 -1486 )  |  -1771
( -53 -824 )  |  -1487
Caped
The Bird Has Made It Home After  2 Turns
The Bird Has Made It Home After  472 Turns
( 636 265 )  |  -673
( 141 59 )  |  -1048
( -685 -607 )  |  -1468
( -1221 -1058 )  |  -2115
( -2175 -1433 )  |  -1258
( -1535 -1305 )  |  -504
( -1048 -1606 )  |  -880
( -1720 -2659 )  |  -1831
( -1526 -2373 )  |  -2219
( -1543 -2978 )  |  -1711
Caped
The Bird Has Made It Home After  6 Turns
( -937 -487 )  |  622
( -334 -266 )  |  -116
( -458 129 )  |  835
( 207 -224 )  |  727
( -553 -473 )  |  604
( -792 -208 )  |  0
( -1738 -268 )  |  -830
( -1746 320 )  |  -1574
( -2228 -455 )  |  -1885
( -2109 -1727 )  |  -1330
Caped
( 50 -1655 )  |  -203
( 39 -2156 )  |  109
( 106 -1324 )  |  694
( -708 -1909 )  |  411
( -402 -2528 )  |  -590
( -252 -2895 )  |  -1741
( -724 -3127 )  |  -2419
( -1507 -2834 )  |  -2665

Simulations in the wild

Simulations are used extremely frequently in real life applications. One of the most common examples of simulations are video games. A games physics engine can accurately simulate objects colliding

Another example is Blender, the software used in 3d animations class, here at Del Norte. Blender is made up of many small simulations, but one big one it uses is simulating the way light bounces off of and interacts with objects.

HW !!!

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

Some ideas:

  • Two objects colliding
  • Gravity on other planets

AND

Find an example of a simulation in a software/game you use, screenshot, and explain how it is a simulation

import random

# Dictionary to simulate the vending machine's inventory
vending_machine = {
    "Coke": 3,
    "Sprite": 5,
    "Water": 10
}

# Function to dispense a drink from the vending machine
def dispense_drink(drink):
    if vending_machine.get(drink) != None: # Check if the drink is in the vending machine's inventory
        if vending_machine[drink] > 0: # Check if the drink is in stock
            vending_machine[drink] -= 1 # Dispense the drink and decrement the stock
            return f"Here is your {drink}"
        else:
            return "Sorry, that drink is currently out of stock."
    else:
        return "Sorry, we don't carry that drink."

# Simulate a customer purchasing a random drink
drink = random.choice(list(vending_machine.keys())) # Choose a random drink from the vending machine's inventory
result = dispense_drink(drink)
print(result)
Here is your Coke

GTA5

Grand Theft Auto V uses simulations to model the behaviors and interactions of pedestrians, traffic, and other objects in the game world. The game's physics engine simulates the movement and collision of objects, and its weather system simulates the effects of weather on the game world.