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
print(f"Dice 1: {random.randint(1,6)}\nDice 2: {random.randint(1,6)}")
Dice 1: 4
Dice 2: 6
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)
G = 9.81
def simulation(height_dropped):
return (f'It will take {((2*height_dropped)/G) ** 0.5} seconds for this to hit the ground.')
simulation(5)
'It will take 1.0096375546923044 seconds for this to hit the ground.'
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 addic.
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.
probability = 0
for i in range(5):
dice_1 = random.randint(1,6)
dice_2 = random.randint(1,6)
if dice_1 + dice_2 >= 9:
probability += 1
print(f'Your chances of winning the jackpot in this scenario was {(probability*100) / 5}%.')
#Output will be 20% most of the time, but can occasionally be something else b/c that's how probability works
Your chances of winning the jackpot in this scenario was 20.0%
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
altitude = 0
speed = 0
fuel = 100
print("Welcome to Flight Simulator!")
while altitude < 10000 and fuel > 0:
altitude_change = random.uniform(-100, 200)
speed_change = random.uniform(-10, 20)
fuel_consumption = random.uniform(1, 5)
altitude += altitude_change
speed += speed_change
fuel -= fuel_consumption
altitude = max(0, altitude)
speed = max(0, speed)
fuel = max(0, fuel)
print(f"Altitude: {altitude:.2f} feet, Speed: {speed:.2f} knots, Fuel: {fuel:.2f} gallons")
if altitude >= 10000:
print("Congratulations! You've reached 10,000 feet.")
else:
print("Out of fuel! The flight has ended.")
Welcome to Flight Simulator!
Altitude: 81.01 feet, Speed: 6.47 knots, Fuel: 98.09 gallons
Altitude: 142.33 feet, Speed: 4.97 knots, Fuel: 95.98 gallons
Altitude: 168.35 feet, Speed: 15.11 knots, Fuel: 91.36 gallons
Altitude: 321.61 feet, Speed: 22.66 knots, Fuel: 86.77 gallons
Altitude: 456.66 feet, Speed: 33.85 knots, Fuel: 85.75 gallons
Altitude: 640.62 feet, Speed: 35.98 knots, Fuel: 82.46 gallons
Altitude: 733.27 feet, Speed: 52.34 knots, Fuel: 81.19 gallons
Altitude: 751.38 feet, Speed: 58.27 knots, Fuel: 79.83 gallons
Altitude: 800.01 feet, Speed: 52.56 knots, Fuel: 78.31 gallons
Altitude: 925.55 feet, Speed: 69.38 knots, Fuel: 75.06 gallons
Altitude: 931.40 feet, Speed: 74.63 knots, Fuel: 73.74 gallons
Altitude: 1115.01 feet, Speed: 72.84 knots, Fuel: 71.91 gallons
Altitude: 1294.09 feet, Speed: 70.52 knots, Fuel: 68.59 gallons
Altitude: 1259.43 feet, Speed: 63.42 knots, Fuel: 66.51 gallons
Altitude: 1229.16 feet, Speed: 71.59 knots, Fuel: 63.94 gallons
Altitude: 1138.34 feet, Speed: 79.92 knots, Fuel: 62.58 gallons
Altitude: 1287.86 feet, Speed: 93.17 knots, Fuel: 57.69 gallons
Altitude: 1277.09 feet, Speed: 112.25 knots, Fuel: 56.03 gallons
Altitude: 1422.81 feet, Speed: 114.99 knots, Fuel: 52.20 gallons
Altitude: 1550.04 feet, Speed: 119.12 knots, Fuel: 48.95 gallons
Altitude: 1533.57 feet, Speed: 117.22 knots, Fuel: 46.52 gallons
Altitude: 1671.29 feet, Speed: 110.36 knots, Fuel: 42.63 gallons
Altitude: 1765.86 feet, Speed: 113.39 knots, Fuel: 40.63 gallons
Altitude: 1826.20 feet, Speed: 122.68 knots, Fuel: 38.96 gallons
Altitude: 1759.27 feet, Speed: 141.32 knots, Fuel: 35.09 gallons
Altitude: 1678.83 feet, Speed: 141.86 knots, Fuel: 32.11 gallons
Altitude: 1631.86 feet, Speed: 159.00 knots, Fuel: 30.48 gallons
Altitude: 1831.32 feet, Speed: 154.89 knots, Fuel: 29.26 gallons
Altitude: 2018.31 feet, Speed: 153.89 knots, Fuel: 27.46 gallons
Altitude: 1945.95 feet, Speed: 167.97 knots, Fuel: 22.66 gallons
Altitude: 1860.35 feet, Speed: 175.88 knots, Fuel: 19.99 gallons
Altitude: 2028.52 feet, Speed: 181.84 knots, Fuel: 18.61 gallons
Altitude: 2023.80 feet, Speed: 193.64 knots, Fuel: 15.82 gallons
Altitude: 2071.35 feet, Speed: 193.61 knots, Fuel: 13.23 gallons
Altitude: 2266.39 feet, Speed: 207.53 knots, Fuel: 9.32 gallons
Altitude: 2433.24 feet, Speed: 211.82 knots, Fuel: 6.27 gallons
Altitude: 2628.64 feet, Speed: 203.33 knots, Fuel: 1.28 gallons
Altitude: 2807.30 feet, Speed: 220.23 knots, Fuel: 0.00 gallons
Out of fuel! The flight has ended.
T or F
- A simulation will always have the same result.
False
T or F
- A simulation investigates a phenomenom without real-world constraints of time, money, or safety.
True
T or F
- A simulation has results which are more accurate than an experiment,
False
T or F
- A simulation can model real-worl events that are not practical for experiments
True
#code
import random
starting_money = 500
jackpot_threshold = 5
winning_sessions = 0
for _ in range(jackpot_threshold):
dice_roll = random.randint(1, 6) + random.randint(1, 6)
if dice_roll <= 3:
starting_money -= 70
elif dice_roll <= 6:
starting_money -= 40
elif dice_roll <= 9:
starting_money += 20
else:
starting_money += 50
if starting_money <= 0:
break
if dice_roll == 12:
starting_money += 100
if starting_money >= jackpot_threshold * 100:
winning_sessions += 1
print(f'Your chances of hitting the jackpot in this scenario were {(winning_sessions * 100) / jackpot_threshold}%.')
Your chances of hitting the jackpot in this scenario were 60.0%.
# Initial parameters
speed = 0 # 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
while distance < 1000:
if speed > max_speed:
speed -= deceleration
distance += speed
else:
speed += acceleration
distance += speed
time += 1
print(f"Time (seconds): {time}, Distance traveled: {distance}, Final speed: {speed}mps")
Time (seconds): 32, Distance traveled: 1053, Final speed: 61mps