""" 1、 A module for generating random numbers """
# Introduce modules import random # Print random numbers by default , Range 0-1 print(random.random()) # Print integer random numbers , Specified scope ( Including the left and right numbers , and range Different ) print(random.randint(1, 8)) # Select one randomly from the specified content , Can be a string , List etc. print(random.choice('abcdef')) # Select randomly from the specified content n individual ( You can specify ), Can be a string , List etc. , The return value is a list print(random.sample('abcdef', 3)) # Print integer random numbers , Specified scope ( Including the left , Excluding the right , and range Same as ) print(random.randrange(1, 8))
Generate verification code
import random def y_code(): # Defined function code = '' # The initial value of the verification code is an empty string for i in range(4): # To generate 4 Bit verification code , So the cycle 4 Time j = random.choice([1, 2]) # Take random number , take 1 Then increase the number , take 2 Then add letters if j == 1: addnum = random.randrange(10) # Take random number code += str(addnum) # Add to verification code else: addal = chr(random.randrange(65, 91)) # Take random number , Convert to letters code += addal # Add to verification code print(code) # Print verification code y_code() # Supplementary information # chr Numbers can be mapped to ASCII Code table character printing , stay ASCII In the code table 65-90 It's the letters print(chr(65)) print(chr(90))