This commit is contained in:
Raymond Hettinger 2016-11-21 14:13:29 -08:00
commit 43dfe2b386
1 changed files with 10 additions and 3 deletions

View File

@ -426,25 +426,32 @@ between the effects of a drug versus a placebo::
Simulation of arrival times and service deliveries in a single server queue:: Simulation of arrival times and service deliveries in a single server queue::
from random import gauss, expovariate from random import expovariate, gauss
from statistics import mean, median, stdev
average_arrival_interval = 5.6 average_arrival_interval = 5.6
average_service_time = 5.0 average_service_time = 5.0
stdev_service_time = 0.5 stdev_service_time = 0.5
num_waiting = 0 num_waiting = 0
arrivals = []
starts = []
arrival = service_end = 0.0 arrival = service_end = 0.0
for i in range(20000): for i in range(20000):
if arrival <= service_end: if arrival <= service_end:
num_waiting += 1 num_waiting += 1
arrival += expovariate(1.0 / average_arrival_interval) arrival += expovariate(1.0 / average_arrival_interval)
print(f'{arrival:6.1f} arrived') arrivals.append(arrival)
else: else:
num_waiting -= 1 num_waiting -= 1
service_start = service_end if num_waiting else arrival service_start = service_end if num_waiting else arrival
service_time = gauss(average_service_time, stdev_service_time) service_time = gauss(average_service_time, stdev_service_time)
service_end = service_start + service_time service_end = service_start + service_time
print(f'\t\t{service_start:.1f} to {service_end:.1f} serviced') starts.append(service_start)
waits = [start - arrival for arrival, start in zip(arrivals, starts)]
print(f'Mean wait: {mean(waits):.1f}. Stdev wait: {stdev(waits):.1f}.')
print(f'Median wait: {median(waits):.1f}. Max wait: {max(waits):.1f}.')
.. seealso:: .. seealso::