]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add comments regarding speed/space/entropy trade-offs (GH-10885)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 4 Dec 2018 08:13:38 +0000 (00:13 -0800)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 4 Dec 2018 08:13:38 +0000 (00:13 -0800)
Lib/random.py

index 4b51b6696bfcd41f914e087bd39d2f3e07ea6a78..a7a86070c0a912c5c9b1b22d816b9d7196aa0cb7 100644 (file)
@@ -333,6 +333,19 @@ class Random(_random.Random):
         # preferred since the list takes less space than the
         # set and it doesn't suffer from frequent reselections.
 
+        # The number of calls to _randbelow() is kept at or near k, the
+        # theoretical minimum.  This is important because running time
+        # is dominated by _randbelow() and because it extracts the
+        # least entropy from the underlying random number generators.
+
+        # Memory requirements are kept to the smaller of a k-length
+        # set or an n-length list.
+
+        # There are other sampling algorithms that do not require
+        # auxiliary memory, but they were rejected because they made
+        # too many calls to _randbelow(), making them slower and
+        # causing them to eat more entropy than necessary.
+
         if isinstance(population, _Set):
             population = tuple(population)
         if not isinstance(population, _Sequence):