Random Module
Random , as the name implies, is used to generate , the specific usage is as follows
Import Randomlis = [1,2,3,4,5,6,7,8,9,10] #随机生成浮点数print (Random.random ()) floating-point number between #随机返回0 to print (Random.uniform (1,10)) Floating-point number between #随机返回1 ~ # randomly generates integer print (Random.randint (1,10)) #随机生成1 the integer print (Random.randrange (0,10,2)) #随机生成0 ~ An even print (Random.randrange (1,10,2)) between the #随机生成1 the cardinality of the # operation sequence, String # #从序列, and the string randomly returns an element print (Random.choice (LIS)) print ( Random.choice (' ABCDEFG ') # #随机截取序列, specified length fragment in string print (Random.sample (lis,2)) print (Random.sample (' ABCDEFG ', 2)) # # Disrupt a sequence random.shuffle (LIS) print (LIS)
The printing results are as follows:
0.476266933939917374.08444480427172258814g[9, 4][' f ', ' a '][7, 6, 1, 2, 10, 3, 4, 8, 5, 9]
Example:
#-*- coding:utf-8 -*-#随机密码生成器, must contain uppercase and lowercase letters, numbers and symbols "(ASCII code) Number: [48,57] lowercase: [97,122] Uppercase: [65,90] Symbol: [33,47 ] "' Import randompass_num = int (Input (" Please enter password length: ")) my_num = pass_num-4mast_list = [] ASCII codes for #用于随机收集大小写字母, numbers, and symbols one ascii_list = [] #用于随机收集剩下的密码字符对应的ASCII码passwd_list = [] # Used to collect characters converted by ASCII code Number = range (48,58) s_letter = range (97,123) B_letter = range ( 65,91) Symbol = range (35,38) def creat_mast (LIS): a = Random.choice (LIS) mast_list.append (a) creat_mast (number) Creat_mast (s_letter) Creat_mast ( B_letter) creat_mast (symbol) random.shuffle (mast_list) def creat_list (LIS): for I in lis: ascii_list.append (i) creat_list (number) creat _list (S_letter) creat_list (b_letter) creat_list (symbol) ascii_list = random.sample (ascii_list,my_num) + mast_listfor i in  ASCII_LIST:    A = CHR (i) passwd_list.append (a) My_ pass = "". Join (passwd_list) print (' Random password:%s '% (My_pass))
Python built-in module--random