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

Skip to content

Commit 311f419

Browse files
committed
Improve comments. Clarify docs.
Replace "type(0)" with "int". Replace "while 1" with "while True"
1 parent 8ddc176 commit 311f419

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

Doc/lib/librandom.tex

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,21 @@ \section{\module{random} ---
182182
\begin{funcdesc}{sample}{population, k}
183183
Return a \var{k} length list of unique elements chosen from the
184184
population sequence. Used for random sampling without replacement.
185+
\versionadded{2.3}
185186

186-
Returns a new list containing elements from the population. The
187-
list itself is in random order so that all sub-slices are also
188-
random samples. The original sequence is left undisturbed.
189-
190-
If the population has repeated elements, then each occurence is a
191-
possible selection in the sample.
187+
Returns a new list containing elements from the population while
188+
leaving the original population unchanged. The resulting list is
189+
in selection order so that all sub-slices will also be valid random
190+
samples. This allows raffle winners (the sample) to be partitioned
191+
into grand prize and second place winners (the subslices).
192192

193-
If indices are needed for a large population, use \function{xrange}
194-
as an argument: \code{sample(xrange(10000000), 60)}.
193+
Members of the population need not be hashable or unique. If the
194+
population contains repeats, then each occurrence is a possible
195+
selection in the sample.
195196

196-
Optional argument random is a 0-argument function returning a random
197-
float in [0.0, 1.0); by default, the standard random.random.
198-
\versionadded{2.3}
197+
To choose a sample from a range of integers, use \function{xrange}
198+
as an argument. This is especially fast and space efficient for
199+
sampling from a large population: \code{sample(xrange(10000000), 60)}.
199200
\end{funcdesc}
200201

201202

Lib/random.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def __whseed(self, x=0, y=0, z=0):
239239
These must be integers in the range [0, 256).
240240
"""
241241

242-
if not type(x) == type(y) == type(z) == type(0):
242+
if not type(x) == type(y) == type(z) == int:
243243
raise TypeError('seeds must be integers')
244244
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
245245
raise ValueError('seeds must be in range(0, 256)')
@@ -407,8 +407,7 @@ def sample(self, population, k, random=None, int=int):
407407
# Previous selections are stored in dictionaries which provide
408408
# __contains__ for detecting repeat selections. Discarding repeats
409409
# is efficient unless most of the population has already been chosen.
410-
# So, tracking selections is useful when sample sizes are much
411-
# smaller than the total population.
410+
# So, tracking selections is fast only with small sample sizes.
412411

413412
n = len(population)
414413
if not 0 <= k <= n:
@@ -417,19 +416,19 @@ def sample(self, population, k, random=None, int=int):
417416
random = self.random
418417
result = [None] * k
419418
if n < 6 * k: # if n len list takes less space than a k len dict
420-
pool = list(population) # track potential selections
421-
for i in xrange(k):
422-
j = int(random() * (n-i)) # non-selected at [0,n-i)
423-
result[i] = pool[j] # save selected element
424-
pool[j] = pool[n-i-1] # non-selected to head of list
419+
pool = list(population)
420+
for i in xrange(k): # invariant: non-selected at [0,n-i)
421+
j = int(random() * (n-i))
422+
result[i] = pool[j]
423+
pool[j] = pool[n-i-1]
425424
else:
426-
selected = {} # track previous selections
425+
selected = {}
427426
for i in xrange(k):
428427
j = int(random() * n)
429-
while j in selected: # discard and replace repeats
428+
while j in selected:
430429
j = int(random() * n)
431430
result[i] = selected[j] = population[j]
432-
return result # return selections in the order they were picked
431+
return result
433432

434433
## -------------------- real-valued distributions -------------------
435434

@@ -455,7 +454,7 @@ def normalvariate(self, mu, sigma):
455454
# Math Software, 3, (1977), pp257-260.
456455

457456
random = self.random
458-
while 1:
457+
while True:
459458
u1 = random()
460459
u2 = random()
461460
z = NV_MAGICCONST*(u1-0.5)/u2
@@ -548,7 +547,7 @@ def vonmisesvariate(self, mu, kappa):
548547
b = (a - _sqrt(2.0 * a))/(2.0 * kappa)
549548
r = (1.0 + b * b)/(2.0 * b)
550549

551-
while 1:
550+
while True:
552551
u1 = random()
553552

554553
z = _cos(_pi * u1)
@@ -595,7 +594,7 @@ def gammavariate(self, alpha, beta):
595594
bbb = alpha - LOG4
596595
ccc = alpha + ainv
597596

598-
while 1:
597+
while True:
599598
u1 = random()
600599
u2 = random()
601600
v = _log(u1/(1.0-u1))/ainv
@@ -616,7 +615,7 @@ def gammavariate(self, alpha, beta):
616615

617616
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
618617

619-
while 1:
618+
while True:
620619
u = random()
621620
b = (_e + alpha)/_e
622621
p = b*u

0 commit comments

Comments
 (0)