Usage of random module

random.random () randomly generates floating-point values ​​between [0, 1)

random.uniform (a, b) Randomly generate interval floating point numbers

random.randint (a, b) randomly generates integers in the specified range [a, b] 

random.randrange (a, b, c) Randomly generate integers in the specified range [a, b) left to right and not to take the specified step c

random.choice (x) generates a specified random string / list / tuples (rows may all iteration ??) the elements

random.sample (x, n) randomly generate the elements in the specified list / string (can it be iterated?) , select n

random.shuffle (list) shuffle the elements in the list

 

Example: Write verification code

def v_code (): 
    ret = '' 
    for i in range (5 ): 
        num = random.randint (0,9)   # chr (85) shows the asc code corresponding to the shaping 
        alf = chr (random.randint (65,122 )) 
        s = str (random.choice ([num, alf])) 
        ret + = s
     return ret

Short version:

def v_code():
    return''.join(str(random.choice([random.randint(0,9),chr(random.randint(65,122))])) for i in range(5))

 

Guess you like

Origin www.cnblogs.com/adelinebao/p/12740465.html