-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_normal_queue.py
45 lines (34 loc) · 1.4 KB
/
simulate_normal_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import random
import numpy as np
from queue import Queue
def simulate_normal_queue(arrival_rate, num_elements):
queue = Queue()
waiting_times = []
for i in range(num_elements):
interarrival_time = random.expovariate(arrival_rate)
arrival_time = i + interarrival_time
# Generate a random waiting time from a normal distribution with mean 0 and standard deviation 1
waiting_time = np.random.normal(0, 1)
# Add the element to the queue with its arrival time and waiting time
queue.put((arrival_time, waiting_time))
while not queue.empty():
arrival_time, waiting_time = queue.get()
# Calculate the waiting time for the current element
waiting_time -= 1
if waiting_time > 0:
# If the waiting time is still positive, put the element back in the queue
queue.put((arrival_time, waiting_time))
else:
# The element has finished waiting
service_time = random.expovariate(1)
departure_time = arrival_time + service_time
waiting_times.append(departure_time - arrival_time)
return waiting_times
# Example usage
arrival_rate = 0.5
num_elements = 100
counter = 1
normal_waiting_times = simulate_normal_queue(arrival_rate, num_elements)
for i in normal_waiting_times:
print("Waiting time for element ", counter, ": ", i, sep="")
counter = counter+1