python 19 random random module

random
first import module
import random

Random decimal
Print (random.random ())   # is greater than a decimal between 0 and less than 1 
Print (random.uniform (l, 3))   # 1 ≤ 3 decimal

Random integer
Print (the random.randint (l, 5))   # is greater than or equal to 1 and less than or equal to integer. 5 
Print (random.randrange (1, 10,))   # 1 or greater and smaller than 10 between the odd

Randomly select a return
Print (The random.choice ([. 1, ' 23 ' , [4,5]])) # . 1 or '23' or [4,5] 
randomly selected plurality of return, the number returned is a function of the second parameter
 Print (random.sample ([1,2,5,8], 2))   # listing any two elements

Disrupt the order of the list
item = [1,3,5,7,9]
random.shuffle(item)
print(item)
random.shuffle(item)
print(item)

Generating a random codes
import random
def v_code():
    code = ''
    for i in range(5):
        num = random.randint(0,9)
        alf = chr(random.randint(65,90))
        add = random.choice([num,alf])
        code = ''.join([code,str(add)])
    return code

print(v_code())

 

Guess you like

Origin www.cnblogs.com/xiuyou/p/11505906.html