summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2016-11-21 22:13:29 (GMT)
committerRaymond Hettinger <python@rcn.com>2016-11-21 22:13:29 (GMT)
commit43dfe2b386eee1d7ca37bc5d9d51e30d3dfa353f (patch)
treed998bc78acc0721b08d86db850ab97c55a836803 /Doc
parentc34e8604c85cf3c9e692a265b879f861dd38e198 (diff)
parent1149d9326e39dd38b2b2a66cfb3fd1d54e7f0777 (diff)
downloadcpython-43dfe2b386eee1d7ca37bc5d9d51e30d3dfa353f.zip
cpython-43dfe2b386eee1d7ca37bc5d9d51e30d3dfa353f.tar.gz
cpython-43dfe2b386eee1d7ca37bc5d9d51e30d3dfa353f.tar.bz2
merge
Diffstat (limited to 'Doc')
-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::