[Python-checkins] python/dist/src/Lib random.py,1.41,1.42

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Fri, 03 Jan 2003 21:20:36 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv476

Modified Files:
	random.py 
Log Message:
Remove the random=None nonsense from sample() before it gets set in stone.
It was once available so that faster generators could be substituted.  Now,
that is less necessary and preferrably done via subclassing.

Also, clarified and shortened the comments for sample().


Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** random.py	29 Dec 2002 23:03:37 -0000	1.41
--- random.py	4 Jan 2003 05:20:33 -0000	1.42
***************
*** 208,212 ****
              x[i], x[j] = x[j], x[i]
  
!     def sample(self, population, k, random=None, int=int):
          """Chooses k unique random elements from a population sequence.
  
--- 208,212 ----
              x[i], x[j] = x[j], x[i]
  
!     def sample(self, population, k, int=int):
          """Chooses k unique random elements from a population sequence.
  
***************
*** 224,251 ****
          This is especially fast and space efficient for sampling from a
          large population:   sample(xrange(10000000), 60)
- 
-         Optional arg random is a 0-argument function returning a random
-         float in [0.0, 1.0); by default, the standard random.random.
          """
  
          # Sampling without replacement entails tracking either potential
!         # selections (the pool) or previous selections.
! 
!         # Pools are stored in lists which provide __getitem__ for selection
!         # and provide a way to remove selections.  But each list.remove()
!         # rebuilds the entire list, so it is better to rearrange the list,
!         # placing non-selected elements at the head of the list.  Tracking
!         # the selection pool is only space efficient with small populations.
  
!         # Previous selections are stored in dictionaries which provide
!         # __contains__ for detecting repeat selections.  Discarding repeats
!         # is efficient unless most of the population has already been chosen.
!         # So, tracking selections is fast only with small sample sizes.
  
          n = len(population)
          if not 0 <= k <= n:
              raise ValueError, "sample larger than population"
!         if random is None:
!             random = self.random
          result = [None] * k
          if n < 6 * k:     # if n len list takes less space than a k len dict
--- 224,244 ----
          This is especially fast and space efficient for sampling from a
          large population:   sample(xrange(10000000), 60)
          """
  
          # Sampling without replacement entails tracking either potential
!         # selections (the pool) in a list or previous selections in a
!         # dictionary.
  
!         # When the number of selections is small compared to the population,
!         # then tracking selections is efficient, requiring only a small
!         # dictionary and an occasional reselection.  For a larger number of
!         # selections, the pool tracking method is preferred since the list takes
!         # less space than the dictionary and it doesn't suffer from frequent
!         # reselections.
  
          n = len(population)
          if not 0 <= k <= n:
              raise ValueError, "sample larger than population"
!         random = self.random
          result = [None] * k
          if n < 6 * k:     # if n len list takes less space than a k len dict
***************
*** 254,258 ****
                  j = int(random() * (n-i))
                  result[i] = pool[j]
!                 pool[j] = pool[n-i-1]
          else:
              selected = {}
--- 247,251 ----
                  j = int(random() * (n-i))
                  result[i] = pool[j]
!                 pool[j] = pool[n-i-1]   # move non-selected item into vacancy
          else:
              selected = {}