Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Monte Carlo Simulation

Submitted to: Sir Gulzaar

Afeef wadood
BITF21M533

Assigment
MONTE CARLO SIMULATION:
Monte Carlo simulation is a statistical technique used for modeling and
analyzing complex systems or processes through random sampling and
probability. It's named after the Monte Carlo Casino in Monaco because
of the element of chance and randomness inherent in the method.

Understanding this technique:


To understand Monte Carlo Simulation Technique, we break it in 5
steps:
 Establishing Probability Distribution
 Cumulative Probability Distribution
 Setting Random Number Intervals
 Generating Random Numbers
 To find answer of required question using above four steps

First Problem:
John is a dentist who schedule all her patients for 30 minutes
appointment. Some of his patients take more or less than 30 minutes
depending on type of dental work to be done.
The following summary shows various categories of work, their possible
& time required to complete work:

Category Time Require No. Of Patients

Filling 45 min 40
crown 60 min 15
cleaning 15 min 15

1|Page
extracting 45 min 10
checkup 15 min 20

Simulate dentist's clinic for four hours & find out average waiting time
for patients as well as illness of doctor. Assume all patients show up at
clinic at exactly their scheduled, arrival time starting at 8:00am.
Use following random numbers for handling above problem: 40, 82, 11,
34, 25, 66, 17, 79

Python Code For this Simulation:


import random
import simpy

# Define the parameters


clinic_opening_time = 8 * 60 # 8:00 AM in minutes
clinic_closing_time = clinic_opening_time + 4 * 60 # 4 hours
appointment_duration = 30 # minutes
appointment_types = ["Filling", "Crown", "Cleaning", "Extracting", "Checkup"]
appointment_durations = [45, 60, 15, 45, 15]

# Function to simulate a patient's visit


def patient(env, name, dentist, arrival_times):
arrival_time = env.now
appointment_type = random.choice(appointment_types)
appointment_duration =
appointment_durations[appointment_types.index(appointment_type)]
with dentist.request() as request:

2|Page
print(f"{name} arrives at {env.now} minutes for a {appointment_type}
appointment.")
yield request

waiting_time = env.now - arrival_time


arrival_times.append(waiting_time) # Store the waiting time
print(f"{name} starts the {appointment_type} appointment at {env.now} minutes
(waiting time: {waiting_time} minutes).")
yield env.timeout(appointment_duration)

print(f"{name} finishes the {appointment_type} appointment at {env.now}


minutes.")

# Function to generate patient arrivals


def generate_patient_arrivals(env, dentist):
while True:
yield env.timeout(appointment_duration)
env.process(patient(env, f"Patient {len(dentist.users)}", dentist, waiting_times))

# Create the simulation environment


env = simpy.Environment()

# Create the dentist resource


dentist = simpy.Resource(env, capacity=1)

# Create a list to store waiting times


waiting_times = []

3|Page
# Start patient arrivals
env.process(generate_patient_arrivals(env, dentist))

# Run the simulation


env.run(until=clinic_closing_time)

# Calculate average waiting time and average idle time


average_waiting_time = sum(waiting_times) / len(waiting_times)
average_idle_time = clinic_closing_time - env.now

# Print the results


print(f"Average waiting time for patients: {average_waiting_time:.2f} min")
print(f"Average idle time for the dentist: {average_idle_time:.2f} min")

OUTPUT:

Average waiting time for patients: 37.89 min


Average idle time for dentist = 0.00 min

Second Problem:
Monte Carlo Simulations to estimate value of PI

Python Code For this Simulation:

4|Page
Using Monte Carlo simulation to estimate the value of π by simulating
random points within a unit square and calculating the ratio of points
inside a quarter of a unit circle.

import random

def monte_carlo_pi(num_samples):
inside_circle = 0
for _ in range(num_samples):
x = random.uniform(0, 1)
y = random.uniform(0, 1)
if x**2 + y**2 <= 1:
inside_circle += 1

return (inside_circle / num_samples) * 4

# Define the number of samples for the simulation


num_samples = 1000000

estimated_pi = monte_carlo_pi(num_samples)
print(f"The estimated value of π using {num_samples} samples: {estimated_pi}")

OUTPUT:

The estimated value of π using 1000000 samples: 3.141396

5|Page

You might also like