#!/usr/bin/python3
class Simulator:
def init(self, service, arrival, sim_time, max_buffer_size = float('inf')):
self.service = 1/service # SERVICE is the average service time; service rate = 1/SERVICE
self.arrival = 1/arrival # ARRIVAL is the average inter-arrival time; arrival rate = 1/ARRIVAL
self.load= self.service / self.arrival # This relationship holds for M/M/1
self.max_buffer_size = max_buffer_size
self.type1 = 1
self.sim_time = sim_time
self.arrivals=0
self.users=0
self.data = None
self.MM1= None
self.FES = None
self.result = None
self.time = None
random.seed(42)
code
Assistant
To modify the service time distribution in the given code, you can simply replace the random.expovariate() function for generating service times with an appropriate function for your desired distribution. Here's how to change the service time distribution to uniform and normal:
I want to modify the service time distribution (it is now exponential). Tell me how to modify the code to use the following distribution as the service time distribution:
where amin and amax are the minimum and maximum values of the uniform distribution, respectively. You can add these values as class attributes in the __init__ method of the Simulator.
Normal Distribution:
Replace the line service_time = random.expovariate(1.0 / self.service) with:
python
service_time = random.gauss(mu, sigma)
where mu and sigma are the mean and standard deviation of the normal distribution, respectively. You can add these values as class attributes in the __init__ method of the Simulator.
Here is the modified code with both uniform and normal distributions:
python
# For uniform distribution
class Simulator:
def __init__(self, service, arrival, sim_time, max_buffer_size = float('inf')):
# ... (other code)
self.amin = amin # Minimum value for uniform distribution
self.amax = amax # Maximum value for uniform distribution
# ... (other methods)
def Arrival(self, time, queue, server_obj):
# ...
service_time = random.uniform(self.amin, self.amax) # Replace with Uniform Distribution
# ...
# ... (rest of the code)
# For normal distribution
class Simulator:
def __init__(self, service, arrival, sim_time, max_buffer_size = float('inf')):
# ... (other code)
self.mu = mu # Mean for normal distribution
self.sigma = sigma # Standard deviation for normal distribution
# ... (other methods)
def Arrival(self, time, queue, server_obj):
# ...
service_time = random.gauss(self.mu, self.sigma) # Replace with Normal Distribution
# ...
# ... (rest of the code)
Alternatively, you can create separate methods for each distribution type in your Simulator class and call the appropriate method depending on the desired distribution.