mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	bpo-33203: Ensure random.choice always raises IndexError on empty sequence (GH-6338)
This commit is contained in:
		
							parent
							
								
									74940913d2
								
							
						
					
					
						commit
						091e95e900
					
				
					 3 changed files with 9 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -242,6 +242,8 @@ def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,
 | 
			
		|||
                "enough bits to choose from a population range this large.\n"
 | 
			
		||||
                "To remove the range limitation, add a getrandbits() method.")
 | 
			
		||||
            return int(random() * n)
 | 
			
		||||
        if n == 0:
 | 
			
		||||
            raise ValueError("Boundary cannot be zero")
 | 
			
		||||
        rem = maxsize % n
 | 
			
		||||
        limit = (maxsize - rem) / maxsize   # int(limit * maxsize) % n == 0
 | 
			
		||||
        r = random()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue