2.random Module
#随机浮点数
Random.random () #生成0到1之间的随机浮点数, cannot be specified by itself
Random.uniform (1,10) # can specify
#随机整数
Random.randint (1,7) #生成1到7之间的随机整数1 <=n<=7
#随机选取0到100间的偶数:
Random.randrange (0,101,2) #生成随机整数
#随机字符
Random.choice (') #传的是一个序列 (including strings, tuples, lists)
#多个字符中选取特定数量的字符
Random.sample (' sequence ', length)
#洗牌
Random.shuffle ([1,2,3,4,5,6,7,8,9])
Example:
1 # 4-bit pure digit verification Code 2 Import Random 3 checkcode='4 for in range (4):5 Current=random.randint (1,9)6 checkcode+=str (current)7 Print(Checkcode)
1 #Digital plus letter verification Code2 ImportRandom3Checkcode="'4 forIinchRange (4):5Current=random.randrange (0,4)6 ifCurrent = =I:7temp = Chr (Random.randint (65,90))8 Else:9temp = Random.randint (0,9)Tencheckcode+=Str (temp) One Print(Checkcode)
Python------module definition, import, optimization------->random module