@@ -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