A simulation is the use of a computer software to represent the dynamic responses of one system by the behaviour of another system modeled after it. A simulation uses a mathematical descriptions, or models, of a real system in the form of a computer program.
Simulation are absractions of more complex objects or phenomena for a specific purpose
Simulations utilize varying sets of values to reflect the changings states of a phenomenon
Simulations work best when the real world experemnts are too impractical or time consuming. For example, simulating how different cars behave when they crash, would be much better than crashng actual cars in the real world, which would be expensive and dangerous.
Simulating something like a dice roll in real life would require accounting for things like: weight, flaws in design, thrust, and gravity.
- KEEP IT SIMPLE! just use a random-number generator! Ignore minor causes of variablility
#imports random module so we can use it in our code
import random
#sets variable random_number as a random number between 1 and 100
random_number = random.randint(1, 100)
#Printing out your random Number
print(random_number)
import random
def flip_coin():
return random.choice(["Heads", "Tails"])
def coin_flip_simulation(num_flips):
heads_count = 0
tails_count = 0
for _ in range(num_flips):
result = flip_coin()
if result == "Heads":
heads_count += 1
else:
tails_count += 1
return heads_count, tails_count
if __name__ == "__main__":
num_flips = 1000 #This is the number of coin flips you want to simulate
heads, tails = coin_flip_simulation(num_flips)
print("Number of Heads: "+ str(heads))
print("Number of Tails: " + str(tails))
print("Heads Probability: "+ str({heads / num_flips}))
print("Tails Probability: "+ str({tails / num_flips}))
Utilize “random” to create a basic simulation of a rolling TWO dice. Print the sum of both dice rolls. Remember to practice good syntax when naming your variables.
import random
#Code, Code, Code
roll1 = random.randint(1, 6)
roll2 = random.randint(1, 6)
print("Roll 1: ", roll1)
print("Roll 2: ", roll2)
Roll 1: 2
Roll 2: 3
Simulations often utilize algorithms and equations to perform tasks because simulations don’t always have the same output
- the output of a simulation depends on the input
An algorithm is a finite sequence of instructions used to solve problems or perform computations.
- commonly used alongside functions
#Defining Function
def algorithm(input):
#Manipulating input and preparing it for the output.
output = input+2
#Return the output
return output
#Call the Function to start the algorithm
algorithm(5)
7
Simulate how long an object will fall for using an algorithm, with user-inputed variables for height dropped. Use the following formula as a reference.
# Constant, Acceleration due to gravity (m/s^2)
import math
G = 9.81
def simulation(height_dropped):
# Code Code Code
time = math.sqrt(2*height_dropped/G)
return time
height = int(input("Enter height of drop in meters: "))
print("Time falling: ", simulation(height))
Time falling: 1.4278431229270645
For loops can also be used in simulations
- They can simulate events that repeat but don’t always have the same output
# Example For Loop
#Creating For Loop to repeat 4 times
for i in range(4):
#Action that happens inside for loop
print("This is run number: " + str(i))
This is run number: 0
This is run number: 1
This is run number: 2
This is run number: 3
You are gambling addict (sigma).
Each session you roll 2 dice.
If your dice roll is greater than or equal to 9 you win the session.
If you win over 5 sessions, you win the jackpot.
Simulate your odds to predict if you will hit the jackpot (how many rounds did you win?) using a for loop and random.
# Code Code Code
for i in range(5):
roll1 = random.randint(1, 6)
roll2 = random.randint(1, 6)
print("Roll 1: ", roll1)
print("Roll 2: ", roll2)
if total >= 9:
print("You won")
print("Total: ", int(total))
print("----------------------------")
else:
print("you should unaddict yourself cuz you suck")
print("Total: ", int(total))
print("----------------------------")
Roll 1: 3
Roll 2: 6
You won
Total: 9
----------------------------
Roll 1: 2
Roll 2: 6
you should unaddict yourself cuz you suck
Total: 8
----------------------------
Roll 1: 1
Roll 2: 4
you should unaddict yourself cuz you suck
Total: 5
----------------------------
Roll 1: 1
Roll 2: 6
you should unaddict yourself cuz you suck
Total: 7
----------------------------
Roll 1: 6
Roll 2: 1
you should unaddict yourself cuz you suck
Total: 7
----------------------------
Welcome to Flight Simulator! Your goal is to complete a Python program that simulates a flight We’ve set up some initial values for altitude, speed, and fuel. Your task is to update these values to make the flight more realistic.
import random
# Initial parameters
print("Welcome to Flight Simulator!")
# Initial values
altitude = 1000 # in feet
speed = 250 # in knots
fuel = 1000 # in gallons
# Simulation loop
while altitude < 10000 and fuel > 0:
# Generate random changes within realistic ranges
altitude_change = random.uniform(-100, 200) # Change in altitude (feet)
speed_change = random.uniform(-10, 20) # Change in speed (knots)
fuel_consumption = random.uniform(5, 15) # Fuel consumption (gallons)
# Update values
altitude += altitude_change
speed += speed_change
fuel -= fuel_consumption
# Ensure realistic bounds for altitude, speed, and fuel
altitude = max(0, altitude) # Altitude should not go below 0
speed = max(0, speed) # Speed should not go below 0
fuel = max(0, fuel) # Fuel should not go below 0
# Display current status
print(f"Altitude: {altitude} feet, Speed: {speed} knots, Fuel: {fuel} gallons")
# Check the flight outcome
if altitude >= 10000:
print("Congratulations! You've reached 10,000 feet.")
else:
print("Out of fuel. Emergency landing required.")
Welcome to Flight Simulator!
Altitude: 956.357192839075 feet, Speed: 241.26305166715437 knots, Fuel: 992.5036827990294 gallons
Altitude: 1007.8708687569114 feet, Speed: 235.315766951723 knots, Fuel: 985.0426122128351 gallons
Altitude: 1066.01708778359 feet, Speed: 246.80334397528748 knots, Fuel: 973.0630059781563 gallons
Altitude: 1262.6866976027459 feet, Speed: 261.6857433822222 knots, Fuel: 960.9944749224235 gallons
Altitude: 1292.1352198943089 feet, Speed: 272.1477159545472 knots, Fuel: 950.7400930900487 gallons
Altitude: 1326.9462997781477 feet, Speed: 280.1914004748991 knots, Fuel: 943.1810765174939 gallons
Altitude: 1361.1129756191044 feet, Speed: 275.78428481245567 knots, Fuel: 933.484355617565 gallons
Altitude: 1426.553472227386 feet, Speed: 275.20327036306094 knots, Fuel: 920.1963068095312 gallons
Altitude: 1475.420472193634 feet, Speed: 288.5045969990335 knots, Fuel: 907.0240418356292 gallons
Altitude: 1435.522768372514 feet, Speed: 300.832869610787 knots, Fuel: 897.0246082000978 gallons
Altitude: 1596.8061376261708 feet, Speed: 308.9072694126601 knots, Fuel: 884.9816218894913 gallons
Altitude: 1573.3890455958183 feet, Speed: 313.989961127708 knots, Fuel: 879.1982713274907 gallons
Altitude: 1714.291971160933 feet, Speed: 314.8261743423903 knots, Fuel: 874.1589197145302 gallons
Altitude: 1676.1663966984574 feet, Speed: 326.9461179406766 knots, Fuel: 863.8262229523481 gallons
Altitude: 1631.1404816838449 feet, Speed: 318.430763661115 knots, Fuel: 856.2584240685007 gallons
Altitude: 1580.4336027407066 feet, Speed: 328.6963813147762 knots, Fuel: 851.1396578422233 gallons
Altitude: 1722.5401688738068 feet, Speed: 329.91366302322086 knots, Fuel: 838.5318864217433 gallons
Altitude: 1678.8670831406114 feet, Speed: 343.1272760763164 knots, Fuel: 829.6131856013964 gallons
Altitude: 1798.9748293193447 feet, Speed: 337.6966055028755 knots, Fuel: 814.6154088382615 gallons
Altitude: 1703.477681675671 feet, Speed: 341.0463940369327 knots, Fuel: 803.1929188169946 gallons
Altitude: 1831.2212083428124 feet, Speed: 355.08067259129064 knots, Fuel: 789.0894262446848 gallons
Altitude: 1998.4054602802462 feet, Speed: 369.9554368415355 knots, Fuel: 780.0182295958916 gallons
Altitude: 2185.1290382081147 feet, Speed: 370.2625762987393 knots, Fuel: 772.3837779778119 gallons
Altitude: 2151.0378837690364 feet, Speed: 371.1434539548215 knots, Fuel: 763.7707925864199 gallons
Altitude: 2258.77717875603 feet, Speed: 379.46960979328014 knots, Fuel: 749.3787715842222 gallons
Altitude: 2404.397756991017 feet, Speed: 387.5089607232442 knots, Fuel: 737.8745430081822 gallons
Altitude: 2451.2979571127353 feet, Speed: 395.45696186138747 knots, Fuel: 722.9166060559863 gallons
Altitude: 2481.9632544851215 feet, Speed: 404.0186199009789 knots, Fuel: 714.441097837335 gallons
Altitude: 2618.60864115804 feet, Speed: 413.28936391123153 knots, Fuel: 704.5746291287353 gallons
Altitude: 2620.8311876522494 feet, Speed: 431.24974177924463 knots, Fuel: 698.37667999113 gallons
Altitude: 2636.302532177071 feet, Speed: 432.70778045835107 knots, Fuel: 686.0861480780328 gallons
Altitude: 2563.972883318871 feet, Speed: 427.00646981828754 knots, Fuel: 680.2220653777777 gallons
Altitude: 2520.448550491567 feet, Speed: 422.4933161726737 knots, Fuel: 672.1122346085398 gallons
Altitude: 2640.235684137433 feet, Speed: 433.32072422375563 knots, Fuel: 665.173519204197 gallons
Altitude: 2734.029565257918 feet, Speed: 450.67143274460966 knots, Fuel: 651.236379859409 gallons
Altitude: 2918.826137690119 feet, Speed: 445.0660005114911 knots, Fuel: 636.8745032046807 gallons
Altitude: 2920.3349549855216 feet, Speed: 449.81843064346236 knots, Fuel: 626.9187224855357 gallons
Altitude: 3036.036866276129 feet, Speed: 464.31694870870086 knots, Fuel: 619.35182245187 gallons
Altitude: 3080.5686455191817 feet, Speed: 465.09143854130167 knots, Fuel: 608.2723597451622 gallons
Altitude: 3108.2751957547725 feet, Speed: 455.6273949306902 knots, Fuel: 597.3645238933531 gallons
Altitude: 3156.9902105855394 feet, Speed: 453.39421104283934 knots, Fuel: 587.1554142624365 gallons
Altitude: 3122.7501739950785 feet, Speed: 456.9764788908006 knots, Fuel: 577.2257042179446 gallons
Altitude: 3304.4523930133632 feet, Speed: 458.7325487161562 knots, Fuel: 567.2448323226023 gallons
Altitude: 3259.301938700186 feet, Speed: 467.863837741783 knots, Fuel: 555.2328237799014 gallons
Altitude: 3170.0659784763093 feet, Speed: 477.0602951379986 knots, Fuel: 549.1158986350626 gallons
Altitude: 3166.12872253454 feet, Speed: 472.3846943845014 knots, Fuel: 539.7932249275171 gallons
Altitude: 3226.2752820369055 feet, Speed: 485.25351056312854 knots, Fuel: 529.0730627945925 gallons
Altitude: 3365.3310155270497 feet, Speed: 486.4832244048225 knots, Fuel: 515.0853627370963 gallons
Altitude: 3437.893360057249 feet, Speed: 488.6249909915639 knots, Fuel: 500.914376830713 gallons
Altitude: 3532.108800226396 feet, Speed: 495.71035879145086 knots, Fuel: 492.5951841228846 gallons
Altitude: 3613.6107580928046 feet, Speed: 495.7143984263731 knots, Fuel: 481.3376563923075 gallons
Altitude: 3564.7819769265498 feet, Speed: 500.5278938245267 knots, Fuel: 468.1699491493145 gallons
Altitude: 3757.3193431636187 feet, Speed: 504.36860518099814 knots, Fuel: 453.25449409289206 gallons
Altitude: 3943.967993765159 feet, Speed: 520.25925825825 knots, Fuel: 446.9203680735685 gallons
Altitude: 4130.4825976055445 feet, Speed: 523.3350137684772 knots, Fuel: 436.72337907807537 gallons
Altitude: 4038.995589123135 feet, Speed: 517.0562997899517 knots, Fuel: 423.8344083294385 gallons
Altitude: 3982.8762354136397 feet, Speed: 527.1111863747803 knots, Fuel: 418.3265162111553 gallons
Altitude: 4036.7081144081635 feet, Speed: 530.906987949448 knots, Fuel: 407.5976993072428 gallons
Altitude: 4190.246852976694 feet, Speed: 535.7369808360255 knots, Fuel: 396.18730492788836 gallons
Altitude: 4238.907874665404 feet, Speed: 551.8367425984513 knots, Fuel: 383.6575902491327 gallons
Altitude: 4277.3102430613835 feet, Speed: 546.282832359971 knots, Fuel: 368.90718581021264 gallons
Altitude: 4410.808071682741 feet, Speed: 554.9736950196202 knots, Fuel: 361.1024571984501 gallons
Altitude: 4517.008863242182 feet, Speed: 570.6733624250294 knots, Fuel: 355.21523703226796 gallons
Altitude: 4501.045133273698 feet, Speed: 580.1641452121273 knots, Fuel: 341.68806386042354 gallons
Altitude: 4532.422521284672 feet, Speed: 590.1187090253881 knots, Fuel: 335.88403209358535 gallons
Altitude: 4556.295303444267 feet, Speed: 599.5092869809581 knots, Fuel: 324.33515261779206 gallons
Altitude: 4508.004930567629 feet, Speed: 601.620726929769 knots, Fuel: 312.1548537103047 gallons
Altitude: 4571.980629593315 feet, Speed: 604.619134720474 knots, Fuel: 305.8522119031688 gallons
Altitude: 4613.651756146988 feet, Speed: 609.5921260888686 knots, Fuel: 299.7397410981073 gallons
Altitude: 4694.375225042651 feet, Speed: 614.560611862531 knots, Fuel: 285.2843435992936 gallons
Altitude: 4806.277949986604 feet, Speed: 604.9692672696677 knots, Fuel: 280.2413245289189 gallons
Altitude: 4787.11504792488 feet, Speed: 623.0111580592747 knots, Fuel: 273.747800668815 gallons
Altitude: 4817.560713971585 feet, Speed: 614.1575761369542 knots, Fuel: 259.3985069198206 gallons
Altitude: 4800.559260823663 feet, Speed: 617.9031684486994 knots, Fuel: 253.7041874205378 gallons
Altitude: 4968.613826773837 feet, Speed: 631.9109610897306 knots, Fuel: 248.60247389631203 gallons
Altitude: 5040.351153753366 feet, Speed: 640.1851109796072 knots, Fuel: 240.76451785160265 gallons
Altitude: 5214.503235859099 feet, Speed: 633.5115706769287 knots, Fuel: 227.5307322151533 gallons
Altitude: 5363.165096670746 feet, Speed: 645.6614536696459 knots, Fuel: 221.541450322942 gallons
Altitude: 5491.136805094978 feet, Speed: 642.1505745894871 knots, Fuel: 207.48888475419307 gallons
Altitude: 5534.63563529659 feet, Speed: 659.9962510689965 knots, Fuel: 194.8980459295856 gallons
Altitude: 5712.964497378958 feet, Speed: 677.5709392061706 knots, Fuel: 181.91320707310118 gallons
Altitude: 5698.104446267142 feet, Speed: 667.7754296924228 knots, Fuel: 172.39444370729134 gallons
Altitude: 5830.8365111988915 feet, Speed: 672.9083194130906 knots, Fuel: 167.26871007314384 gallons
Altitude: 5747.038062632784 feet, Speed: 689.2803599187688 knots, Fuel: 158.75843310971914 gallons
Altitude: 5738.157758177382 feet, Speed: 702.346619218364 knots, Fuel: 153.0020909912094 gallons
Altitude: 5824.700785974987 feet, Speed: 702.8590045147262 knots, Fuel: 147.5311073994458 gallons
Altitude: 5871.504202614372 feet, Speed: 715.3188031759917 knots, Fuel: 133.28779677426152 gallons
Altitude: 5793.409097012715 feet, Speed: 733.9060465960949 knots, Fuel: 126.04679853032003 gallons
Altitude: 5815.663431734294 feet, Speed: 735.0332833547508 knots, Fuel: 117.06874835122437 gallons
Altitude: 5940.695052725514 feet, Speed: 741.0234233138525 knots, Fuel: 109.68286760532125 gallons
Altitude: 5996.215292667413 feet, Speed: 757.7078589744426 knots, Fuel: 97.27214449092563 gallons
Altitude: 5921.149130064063 feet, Speed: 753.5467429861744 knots, Fuel: 84.44436348261964 gallons
Altitude: 5969.455693115649 feet, Speed: 749.6100139269672 knots, Fuel: 71.74099772992115 gallons
Altitude: 6092.691591326852 feet, Speed: 768.2370287239031 knots, Fuel: 66.50709679941818 gallons
Altitude: 6095.02054080661 feet, Speed: 764.7541777022712 knots, Fuel: 55.72854923870749 gallons
Altitude: 6250.790566072456 feet, Speed: 756.5245980717584 knots, Fuel: 48.05741434946711 gallons
Altitude: 6309.7681341472935 feet, Speed: 768.2966510088007 knots, Fuel: 36.57899835543338 gallons
Altitude: 6481.253878890723 feet, Speed: 776.6489402029364 knots, Fuel: 27.638754431501397 gallons
Altitude: 6630.932233956663 feet, Speed: 788.5117713467736 knots, Fuel: 14.62168730933365 gallons
Altitude: 6816.099529113105 feet, Speed: 800.0531307807416 knots, Fuel: 0.3067095927738226 gallons
Altitude: 6887.172419463068 feet, Speed: 805.8330031765428 knots, Fuel: 0 gallons
Out of fuel. Emergency landing required.
T or F
- A simulation will always have the same result. T or F
- A simulation investigates a phenomenom without real-world constraints of time, bank, or safety. T or F
- A simulation has results which are more accurate than an experiment, T or F
- A simulation can model real-worl events that are not practical for experiments
First finish Popcorn Hack #3. Expand the simulation to involve your own bank.
starting bank: $100
(Dice Roll <= 3) → lose $70
( 6> Dice Roll >3) → lose $40
( 9> Dice Roll >=6) → win $20
( Dice Roll>= 9 + Session Win) → win $50
Jackpot → win $100
bank = 100
# Code Code Code
for i in range(5):
roll1 = random.randint(1, 6)
roll2 = random.randint(1, 6)
print("Roll 1: ", roll1)
print("Roll 2: ", roll2)
total = roll1 + roll2
if total >= 9:
print("You won")
print("Total: ", int(total))
bank += 50
print("Bank: ", int(bank))
print("----------------------------")
elif total < 9 and total >= 6:
print("You won")
print("Total: ", int(total))
bank += 20
print("Bank: ", int(bank))
print("----------------------------")
elif total < 6 and total > 3:
print("You won")
print("Total: ", int(total))
bank -= 40
print("Bank: ", int(bank))
print("----------------------------")
elif total <= 3:
print("you should unaddict yourself cuz you suck")
print("Total: ", int(total))
bank -= 70
print("Bank: ", int(bank))
print("----------------------------")
Roll 1: 2
Roll 2: 5
You won
Total: 7
Bank: 120
----------------------------
Roll 1: 5
Roll 2: 4
You won
Total: 9
Bank: 170
----------------------------
Roll 1: 6
Roll 2: 1
You won
Total: 7
Bank: 190
----------------------------
Roll 1: 6
Roll 2: 1
You won
Total: 7
Bank: 210
----------------------------
Roll 1: 6
Roll 2: 1
You won
Total: 7
Bank: 230
----------------------------
Given initial parameters for a car simulation, including its initial speed, acceleration rate, deceleration rate, maximum speed, and initial distance, write a program to simulate the car’s journey and determine the final speed, distance covered, and time taken before it either covers 1000 meters or slows down to below 5 m/s?
# Initial parameters
speed = 20 # Initial speed
acceleration = 2 # Acceleration rate in m/s^2
deceleration = 1 # Deceleration rate in m/s^2
max_speed = 60 # Maximum speed in m/s
distance = 0 # Initial distance
time = 0 # Initial time
#Code Code Code
def car_simulation(initial_speed, acceleration_rate, deceleration_rate, max_speed, initial_distance):
current_speed = initial_speed
current_distance = initial_distance
time = 0
while current_distance < 1000 and current_speed >= 5:
# Update time
time += 1
# Calculate new speed based on acceleration and deceleration
if current_speed < max_speed:
current_speed += acceleration_rate
else:
current_speed -= deceleration_rate
# Calculate the distance covered in this time step
current_distance += current_speed
return current_speed, current_distance, time
final_speed, distance_covered, time_taken = car_simulation(speed, acceleration, deceleration, max_speed, distance)
print(f"Final Speed: {final_speed} m/s")
print(f"Distance Covered: {distance_covered} meters")
print(f"Time Taken: {time_taken} seconds")
Final Speed: 60 m/s
Distance Covered: 1000 meters
Time Taken: 23 seconds