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, sudying 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 2.962348764761103 seconds for an object that weighs 167 pounds to fall  43.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)
3.0000000000000013
44.10000000000004

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 randomy 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  4 Turns
( 500 -18 )
( 141 -905 )
( -374 -1064 )
( -738 -1990 )
( -167 -945 )
( -728 76 )
( -78 20 )
( 274 -394 )
( 1414 1182 )
( 2251 1797 )
Caped
The Man Has Made It Home After  66 Turns
The Man Has Made It Home After  45354 Turns
The Man Has Made It Home After  8 Turns
The Man Has Made It Home After  20 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
( -1639 -987 )
( -1879 -1783 )
( -1308 -1380 )
( -2099 -1505 )
( -3565 -763 )
( -3524 -160 )
( -3147 107 )
( -2488 1632 )
( -3522 1318 )
( -3292 2326 )
Caped
The Man Has Made It Home After  841340 Turns
The Man Has Made It Home After  156 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  4 Turns
( -213 501 )
( -503 673 )
( -440 -850 )
( -791 -595 )
( -1583 -1153 )
( -2750 -2260 )
( -2452 -2456 )
( -2191 -3571 )
( -2458 -4410 )
( -3021 -5621 )
Caped
The Man Has Made It Home After  10 Turns
The Man Has Made It Home After  4 Turns
( 383 -1433 )
( -506 -416 )
( 263 -1167 )
( -263 -979 )
( 331 189 )
( 1494 772 )
( 319 473 )
( 169 677 )
( -1257 -493 )
( -1662 -828 )
Caped
The Man Has Made It Home After  8 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  36906 Turns
( 775 215 )
( -174 810 )
( -1679 645 )
( -1630 -46 )
( -884 -1192 )
( -669 -327 )
( -30 -1432 )
( 146 -724 )
( -439 -1267 )
( 500 -212 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  74690 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  8 Turns
The Man Has Made It Home After  14 Turns
The Man Has Made It Home After  2 Turns
( 786 -22 )
( 474 1070 )
( 1317 993 )
( 913 -393 )
( 1043 -427 )
( 958 -118 )
( 1637 -51 )
( 1903 -1499 )
( 1849 -2091 )
( 1912 -2554 )
Caped
The Man Has Made It Home After  8 Turns
( 659 435 )
( 1725 1011 )
( 1774 1066 )
( 1534 1546 )
( 1992 682 )
( 2350 -206 )
( 2253 -315 )
( 2204 -512 )
( 2741 -295 )
( 4575 -367 )
Caped
The Man Has Made It Home After  1932 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  430 Turns
The Man Has Made It Home After  2080 Turns
The Man Has Made It Home After  448 Turns
The Man Has Made It Home After  23332 Turns
The Man Has Made It Home After  5450 Turns
The Man Has Made It Home After  22 Turns
The Man Has Made It Home After  729852 Turns
The Man Has Made It Home After  2 Turns
( -372 554 )
( -1408 520 )
( -1535 425 )
( -713 1059 )
( -645 -39 )
( -698 90 )
( -481 49 )
( 273 -665 )
( 42 -812 )
( 234 -578 )
Caped
The Man Has Made It Home After  16532 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  39142 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  29540 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  10 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  44 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  1242 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  188 Turns
The Man Has Made It Home After  214 Turns
The Man Has Made It Home After  6 Turns
( 46 -380 )
( -499 -585 )
( -371 -1509 )
( 1298 -1756 )
( 1484 -1572 )
( 2301 -1011 )
( 2473 -1833 )
( 2744 -72 )
( 3380 -134 )
( 2229 -1445 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  1096 Turns
The Man Has Made It Home After  24826 Turns
The Man Has Made It Home After  2 Turns
( 381 -1099 )
( 885 -939 )
( -343 -2047 )
( -164 -1608 )
( -589 -429 )
( -737 -253 )
( -1210 370 )
( -2297 407 )
( -2190 436 )
( -1946 824 )
Caped
( 1018 70 )
( 414 322 )
( 533 -405 )
( -756 -1036 )
( -1384 -2498 )
( -1472 -3710 )
( -752 -4740 )
( -1347 -4221 )
( -599 -3799 )
( 180 -4224 )
Caped
The Man Has Made It Home After  120 Turns
The Man Has Made It Home After  82 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2452 Turns
The Man Has Made It Home After  39848 Turns
( -84 298 )
( 1150 1264 )
( 451 1127 )
( 1060 680 )
( 729 -357 )
( 693 -289 )
( 868 -170 )
( 1117 -521 )
( 334 -962 )
( 1079 -1917 )
Caped
The Man Has Made It Home After  54 Turns
( 1184 226 )
( 1811 903 )
( 1698 -4 )
( 2275 1255 )
( 1620 1794 )
( 1926 690 )
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb Cell 10 in <cell line: 9>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a> if step == 3:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=17'>18</a>     y = y-1
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=19'>20</a> turn = turn + 1
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=21'>22</a> if x == 0 and y == 0:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X12sdnNjb2RlLXJlbW90ZQ%3D%3D?line=22'>23</a>     nights = nights + 1

KeyboardInterrupt: 
import random
x = 0
y = 0
z = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    #rando 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)
( 528 -66 )  |  -6
( 253 -661 )  |  -772
( -770 -894 )  |  -800
( -75 -505 )  |  -1346
( 1494 -931 )  |  -1851
( 2295 -23 )  |  -2082
( 2596 -885 )  |  -3285
( 2697 -4 )  |  -4209
( 2026 588 )  |  -3356
( 1881 681 )  |  -3444
Caped
( -318 -898 )  |  336
( 666 -2291 )  |  -565
( 473 -1061 )  |  668
( 682 -1770 )  |  -94
( 341 -1240 )  |  -415
( 1336 -490 )  |  -1028
( 1684 98 )  |  -126
( 1814 4 )  |  794
( 2230 103 )  |  379
( 2725 344 )  |  443
Caped
( -728 185 )  |  47
( -898 284 )  |  124
( -347 425 )  |  82
( -386 352 )  |  46
( 292 735 )  |  -443
( 758 1497 )  |  -1005
( 1795 1335 )  |  -1126
( 1571 1942 )  |  -531
( 982 2371 )  |  -667
( 1944 2785 )  |  -565
Caped
( 1130 -990 )  |  -44
( 274 -484 )  |  -706
( 105 -729 )  |  -548
( -554 -275 )  |  -615
( -1168 -582 )  |  -572
( -1253 -363 )  |  -896
( -1267 -90 )  |  -1011
( -1760 -239 )  |  -1315
( -977 -833 )  |  -1888
( -1331 -796 )  |  -1905
Caped
( 22 -111 )  |  19
( -1096 -15 )  |  -313
( -1440 358 )  |  1076
( -2455 195 )  |  1016
( -3012 722 )  |  1102
( -2453 586 )  |  1691
( -2075 -297 )  |  1870
( -2819 -577 )  |  1574
( -3241 -1215 )  |  1428
( -3121 -1819 )  |  1544
Caped
( -199 -310 )  |  125
( -104 -378 )  |  980
( 310 -1175 )  |  859
( 1430 -1852 )  |  -202
( 1143 -1744 )  |  -335
( 828 -1573 )  |  -311
( 680 -679 )  |  -733
( 338 -1318 )  |  -1532
( 181 -1003 )  |  -1824
( -724 -1068 )  |  -1708
Caped
( 39 631 )  |  -60
( -1923 755 )  |  -394
( -2050 1763 )  |  -787
( -1770 2773 )  |  -201
( -1949 2984 )  |  527
( -2318 2809 )  |  -61
( -2953 3906 )  |  -89
( -2622 2962 )  |  574
( -2778 3837 )  |  447
( -3072 4274 )  |  636
Caped
The Bird Has Made It Home After  2 Turns
( 388 -18 )  |  -562
( -566 -380 )  |  -248
( -816 -532 )  |  794
( -510 -462 )  |  414
( -1520 -793 )  |  -1057
( -1369 -1809 )  |  -1080
( -1481 -1896 )  |  -255
( -914 -1281 )  |  -385
( -68 -1581 )  |  -453
( -1091 -1790 )  |  237
Caped
The Bird Has Made It Home After  38 Turns
( -514 265 )  |  239
( 19 26 )  |  283
( -103 595 )  |  256
( 559 -70 )  |  -19
( 262 -362 )  |  -346
( -787 -806 )  |  -317
( 273 -599 )  |  -1078
( 68 -342 )  |  -1146
( -533 -970 )  |  -321
( -457 -1001 )  |  -392
Caped
( 1050 229 )  |  -1823
( -790 1194 )  |  -2620
( -668 2423 )  |  -2189
( -525 1141 )  |  -2372
( -410 648 )  |  -2776
( -64 594 )  |  -2798
( -948 305 )  |  -3207
( -878 43 )  |  -3621
( -592 180 )  |  -3418
( -43 171 )  |  -3722
Caped
The Bird Has Made It Home After  2 Turns
( -757 -647 )  |  -306
( -897 -792 )  |  -1049
( -475 -291 )  |  -336
( 146 1275 )  |  -263
( 245 472 )  |  319
( 826 973 )  |  -385
( 2037 569 )  |  336
( 2251 630 )  |  -205
( 3303 777 )  |  -120
( 2326 1863 )  |  573
Caped
( -16 -688 )  |  260
( 416 -1002 )  |  242
( 703 -1184 )  |  623
( 463 -1307 )  |  -168
( 348 -654 )  |  -524
( 703 -601 )  |  -1434
( 738 -362 )  |  -1220
( 1375 -101 )  |  -830
( 1286 -841 )  |  -421
( 924 -605 )  |  -1209
Caped
The Bird Has Made It Home After  2 Turns
( -76 479 )  |  -349
( -1103 473 )  |  -170
( -1347 -144 )  |  -183
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb Cell 11 in <cell line: 10>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=11'>12</a> step = random.randrange(6)
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=12'>13</a> if step == 0:
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a>     x = x+1
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> if step == 1:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/akshat1228/vscode/vscode/fastpages/_notebooks/2022-12-13-Lesson7.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a>     x = x-1

KeyboardInterrupt: 

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

HW!!

import random

# Initial position
x = 0
y = 0

# Initial velocity
vx = random.uniform(-10, 10)  # random value between -10 and 10 for the x-velocity
vy = random.uniform(-10, 10)  # random value between -10 and 10 for the y-velocity
while True:
    # Update position
    x += vx
    y += vy

    # Check if ball has bounced off the edge of the screen
    if x < 0 or x > 100:
        vx *= -1  # reverse x-velocity
    if y < 0 or y > 100:
        vy *= -1  # reverse y-velocity

    # Output current position
    print(f"Position: ({x}, {y})")

IF I RUN IT, IT WILL CRASH

Example of a simulation

This is NBA2k

It's a simulation based from NBA2k23, you can modify your player with all sorts of factors like jump shot and speed.

I enjoy using it because I like basketball a lot.

This is a simulation because it shows the result for what the video gamer chooses and how they want their player to be.