Designated digit randomly generated PIN

 
 
import random
import string

# 方法一:
def code_1(m, choice):
code=''.join(random.sample(choice, m))
return code
print(code_1(4, string.ascii_letters + string.digits))
 
 
# 方法二:
def code_2(n):
code=''
for i in range(n):
number=random.randint(0, 9) # 0-9
lower_char=chr(random.randint(97, 122)) # a-z
upper_char=chr(random.randint(65, 90)) # A-Z
char_1=random.choice([number, lower_char, upper_char])
code+=str(char_1)
return code

print(code_2(4)
 

First the code. . . As I ^ ^ efficient ... Discolored, do not mind ha ~

Random Comments

Generating a random integer between 1-100

print(random.randint(1,100))

A randomly generated between the odd-numbered 0-100

print (random.randrange (0,100,2))

A randomly generated between the even-numbered 0-100

print (random.randrange (1, 100, 2))

Generates a random float

print(random.random())
print(random.uniform(1,10))

Randomly select a character

print(random.choice(r'qwertyuiop[]\asdfghjkl;zxcvbnm,./'))

 Randomly generated specified number of characters

print(code=''.join(random.sample(choice, m)))

 Wherein the number of codes for the alternative choice string, m is generated

 

Guess you like

Origin www.cnblogs.com/aqin1012/p/11615371.html