[Python-checkins] python/dist/src/Lib random.py,1.38,1.39
rhettinger@projects.sourceforge.net
rhettinger@projects.sourceforge.net
Mon, 18 Nov 2002 01:01:28 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv29985/Lib
Modified Files:
random.py
Log Message:
Improve comments. Clarify docs.
Replace "type(0)" with "int".
Replace "while 1" with "while True"
Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.38
retrieving revision 1.39
diff -C2 -d -r1.38 -r1.39
*** random.py 13 Nov 2002 15:26:37 -0000 1.38
--- random.py 18 Nov 2002 09:01:21 -0000 1.39
***************
*** 240,244 ****
"""
! if not type(x) == type(y) == type(z) == type(0):
raise TypeError('seeds must be integers')
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
--- 240,244 ----
"""
! if not type(x) == type(y) == type(z) == int:
raise TypeError('seeds must be integers')
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
***************
*** 408,413 ****
# __contains__ for detecting repeat selections. Discarding repeats
# is efficient unless most of the population has already been chosen.
! # So, tracking selections is useful when sample sizes are much
! # smaller than the total population.
n = len(population)
--- 408,412 ----
# __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)
***************
*** 418,434 ****
result = [None] * k
if n < 6 * k: # if n len list takes less space than a k len dict
! pool = list(population) # track potential selections
! for i in xrange(k):
! j = int(random() * (n-i)) # non-selected at [0,n-i)
! result[i] = pool[j] # save selected element
! pool[j] = pool[n-i-1] # non-selected to head of list
else:
! selected = {} # track previous selections
for i in xrange(k):
j = int(random() * n)
! while j in selected: # discard and replace repeats
j = int(random() * n)
result[i] = selected[j] = population[j]
! return result # return selections in the order they were picked
## -------------------- real-valued distributions -------------------
--- 417,433 ----
result = [None] * k
if n < 6 * k: # if n len list takes less space than a k len dict
! pool = list(population)
! for i in xrange(k): # invariant: non-selected at [0,n-i)
! j = int(random() * (n-i))
! result[i] = pool[j]
! pool[j] = pool[n-i-1]
else:
! selected = {}
for i in xrange(k):
j = int(random() * n)
! while j in selected:
j = int(random() * n)
result[i] = selected[j] = population[j]
! return result
## -------------------- real-valued distributions -------------------
***************
*** 456,460 ****
random = self.random
! while 1:
u1 = random()
u2 = random()
--- 455,459 ----
random = self.random
! while True:
u1 = random()
u2 = random()
***************
*** 549,553 ****
r = (1.0 + b * b)/(2.0 * b)
! while 1:
u1 = random()
--- 548,552 ----
r = (1.0 + b * b)/(2.0 * b)
! while True:
u1 = random()
***************
*** 596,600 ****
ccc = alpha + ainv
! while 1:
u1 = random()
u2 = random()
--- 595,599 ----
ccc = alpha + ainv
! while True:
u1 = random()
u2 = random()
***************
*** 617,621 ****
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
! while 1:
u = random()
b = (_e + alpha)/_e
--- 616,620 ----
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
! while True:
u = random()
b = (_e + alpha)/_e