Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1149d93

Browse files
committed
Add analysis section to motivate the single server queue example
1 parent e132910 commit 1149d93

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

Doc/library/random.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,25 +426,32 @@ between the effects of a drug versus a placebo::
426426

427427
Simulation of arrival times and service deliveries in a single server queue::
428428

429-
from random import gauss, expovariate
429+
from random import expovariate, gauss
430+
from statistics import mean, median, stdev
430431

431432
average_arrival_interval = 5.6
432433
average_service_time = 5.0
433434
stdev_service_time = 0.5
434435

435436
num_waiting = 0
437+
arrivals = []
438+
starts = []
436439
arrival = service_end = 0.0
437440
for i in range(20000):
438441
if arrival <= service_end:
439442
num_waiting += 1
440443
arrival += expovariate(1.0 / average_arrival_interval)
441-
print(f'{arrival:6.1f} arrived')
444+
arrivals.append(arrival)
442445
else:
443446
num_waiting -= 1
444447
service_start = service_end if num_waiting else arrival
445448
service_time = gauss(average_service_time, stdev_service_time)
446449
service_end = service_start + service_time
447-
print(f'\t\t{service_start:.1f} to {service_end:.1f} serviced')
450+
starts.append(service_start)
451+
452+
waits = [start - arrival for arrival, start in zip(arrivals, starts)]
453+
print(f'Mean wait: {mean(waits):.1f}. Stdev wait: {stdev(waits):.1f}.')
454+
print(f'Median wait: {median(waits):.1f}. Max wait: {max(waits):.1f}.')
448455

449456
.. seealso::
450457

0 commit comments

Comments
 (0)