summaryrefslogtreecommitdiffstats
path: root/Doc/library/random.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/random.rst')
-rw-r--r--Doc/library/random.rst13
1 files changed, 10 insertions, 3 deletions
diff --git a/Doc/library/random.rst b/Doc/library/random.rst
index 4d0c8bc..b0f8194 100644
--- a/Doc/library/random.rst
+++ b/Doc/library/random.rst
@@ -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::
- from random import gauss, expovariate
+ from random import expovariate, gauss
+ from statistics import mean, median, stdev
average_arrival_interval = 5.6
average_service_time = 5.0
stdev_service_time = 0.5
num_waiting = 0
+ arrivals = []
+ starts = []
arrival = service_end = 0.0
for i in range(20000):
if arrival <= service_end:
num_waiting += 1
arrival += expovariate(1.0 / average_arrival_interval)
- print(f'{arrival:6.1f} arrived')
+ arrivals.append(arrival)
else:
num_waiting -= 1
service_start = service_end if num_waiting else arrival
service_time = gauss(average_service_time, stdev_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::