@@ -167,15 +167,11 @@ def seed(self, a=None, version=2):
167167 elif version == 2 and isinstance (a , (str , bytes , bytearray )):
168168 if isinstance (a , str ):
169169 a = a .encode ()
170- a = int .from_bytes (a + _sha512 (a ).digest (), 'big' )
170+ a = int .from_bytes (a + _sha512 (a ).digest ())
171171
172172 elif not isinstance (a , (type (None ), int , float , str , bytes , bytearray )):
173- _warn ('Seeding based on hashing is deprecated\n '
174- 'since Python 3.9 and will be removed in a subsequent '
175- 'version. The only \n '
176- 'supported seed types are: None, '
177- 'int, float, str, bytes, and bytearray.' ,
178- DeprecationWarning , 2 )
173+ raise TypeError ('The only supported seed types are: None,\n '
174+ 'int, float, str, bytes, and bytearray.' )
179175
180176 super ().seed (a )
181177 self .gauss_next = None
@@ -250,10 +246,8 @@ def __init_subclass__(cls, /, **kwargs):
250246 break
251247
252248 def _randbelow_with_getrandbits (self , n ):
253- "Return a random int in the range [0,n). Returns 0 if n== 0."
249+ "Return a random int in the range [0,n). Defined for n > 0."
254250
255- if not n :
256- return 0
257251 getrandbits = self .getrandbits
258252 k = n .bit_length () # don't use (n-1) here because n can be 1
259253 r = getrandbits (k ) # 0 <= r < 2**k
@@ -262,7 +256,7 @@ def _randbelow_with_getrandbits(self, n):
262256 return r
263257
264258 def _randbelow_without_getrandbits (self , n , maxsize = 1 << BPF ):
265- """Return a random int in the range [0,n). Returns 0 if n== 0.
259+ """Return a random int in the range [0,n). Defined for n > 0.
266260
267261 The implementation does not use getrandbits, but only random.
268262 """
@@ -273,8 +267,6 @@ def _randbelow_without_getrandbits(self, n, maxsize=1<<BPF):
273267 "enough bits to choose from a population range this large.\n "
274268 "To remove the range limitation, add a getrandbits() method." )
275269 return _floor (random () * n )
276- if n == 0 :
277- return 0
278270 rem = maxsize % n
279271 limit = (maxsize - rem ) / maxsize # int(limit * maxsize) % n == 0
280272 r = random ()
@@ -303,10 +295,10 @@ def randbytes(self, n):
303295 ## -------------------- integer methods -------------------
304296
305297 def randrange (self , start , stop = None , step = _ONE ):
306- """Choose a random item from range(start, stop[, step]).
298+ """Choose a random item from range(stop) or range( start, stop[, step]).
307299
308- This fixes the problem with randint() which includes the
309- endpoint; in Python this is usually not what you want .
300+ Roughly equivalent to ``choice(range(start, stop, step))`` but
301+ supports arbitrarily large ranges and is optimized for common cases .
310302
311303 """
312304
@@ -387,37 +379,24 @@ def randint(self, a, b):
387379
388380 def choice (self , seq ):
389381 """Choose a random element from a non-empty sequence."""
390- # raises IndexError if seq is empty
391- return seq [self ._randbelow (len (seq ))]
392-
393- def shuffle (self , x , random = None ):
394- """Shuffle list x in place, and return None.
395382
396- Optional argument random is a 0-argument function returning a
397- random float in [0.0, 1.0); if it is the default None, the
398- standard random.random will be used.
383+ # As an accommodation for NumPy, we don't use "if not seq"
384+ # because bool(numpy.array()) raises a ValueError.
385+ if not len (seq ):
386+ raise IndexError ('Cannot choose from an empty sequence' )
387+ return seq [self ._randbelow (len (seq ))]
399388
400- """
389+ def shuffle (self , x ):
390+ """Shuffle list x in place, and return None."""
401391
402- if random is None :
403- randbelow = self ._randbelow
404- for i in reversed (range (1 , len (x ))):
405- # pick an element in x[:i+1] with which to exchange x[i]
406- j = randbelow (i + 1 )
407- x [i ], x [j ] = x [j ], x [i ]
408- else :
409- _warn ('The *random* parameter to shuffle() has been deprecated\n '
410- 'since Python 3.9 and will be removed in a subsequent '
411- 'version.' ,
412- DeprecationWarning , 2 )
413- floor = _floor
414- for i in reversed (range (1 , len (x ))):
415- # pick an element in x[:i+1] with which to exchange x[i]
416- j = floor (random () * (i + 1 ))
417- x [i ], x [j ] = x [j ], x [i ]
392+ randbelow = self ._randbelow
393+ for i in reversed (range (1 , len (x ))):
394+ # pick an element in x[:i+1] with which to exchange x[i]
395+ j = randbelow (i + 1 )
396+ x [i ], x [j ] = x [j ], x [i ]
418397
419398 def sample (self , population , k , * , counts = None ):
420- """Chooses k unique random elements from a population sequence or set .
399+ """Chooses k unique random elements from a population sequence.
421400
422401 Returns a new list containing elements from the population while
423402 leaving the original population unchanged. The resulting list is
@@ -470,13 +449,8 @@ def sample(self, population, k, *, counts=None):
470449 # causing them to eat more entropy than necessary.
471450
472451 if not isinstance (population , _Sequence ):
473- if isinstance (population , _Set ):
474- _warn ('Sampling from a set deprecated\n '
475- 'since Python 3.9 and will be removed in a subsequent version.' ,
476- DeprecationWarning , 2 )
477- population = tuple (population )
478- else :
479- raise TypeError ("Population must be a sequence. For dicts or sets, use sorted(d)." )
452+ raise TypeError ("Population must be a sequence. "
453+ "For dicts or sets, use sorted(d)." )
480454 n = len (population )
481455 if counts is not None :
482456 cum_counts = list (_accumulate (counts ))
@@ -580,7 +554,7 @@ def triangular(self, low=0.0, high=1.0, mode=None):
580554 low , high = high , low
581555 return low + (high - low ) * _sqrt (u * c )
582556
583- def normalvariate (self , mu , sigma ):
557+ def normalvariate (self , mu = 0.0 , sigma = 1.0 ):
584558 """Normal distribution.
585559
586560 mu is the mean, and sigma is the standard deviation.
@@ -601,7 +575,7 @@ def normalvariate(self, mu, sigma):
601575 break
602576 return mu + z * sigma
603577
604- def gauss (self , mu , sigma ):
578+ def gauss (self , mu = 0.0 , sigma = 1.0 ):
605579 """Gaussian distribution.
606580
607581 mu is the mean, and sigma is the standard deviation. This is
@@ -833,15 +807,15 @@ class SystemRandom(Random):
833807 """
834808
835809 def random (self ):
836- """Get the next random number in the range [ 0.0, 1.0) ."""
837- return (int .from_bytes (_urandom (7 ), 'big' ) >> 3 ) * RECIP_BPF
810+ """Get the next random number in the range 0.0 <= X < 1.0."""
811+ return (int .from_bytes (_urandom (7 )) >> 3 ) * RECIP_BPF
838812
839813 def getrandbits (self , k ):
840814 """getrandbits(k) -> x. Generates an int with k random bits."""
841815 if k < 0 :
842816 raise ValueError ('number of bits must be non-negative' )
843817 numbytes = (k + 7 ) // 8 # bits / 8 and rounded up
844- x = int .from_bytes (_urandom (numbytes ), 'big' )
818+ x = int .from_bytes (_urandom (numbytes ))
845819 return x >> (numbytes * 8 - k ) # trim excess bits
846820
847821 def randbytes (self , n ):
0 commit comments