random module

  Here I paste someone else's, come and find it if you need it

Basic usage of random module

# !/usr/bin/env python 
# _*_encoding: utf-8_*_ 
import random
 print (random.random())   # 0.6445010863311293   
# random.random() is used to generate a 0 to 1 random number: 0 <= n < 1.0 
print (random.randint(1,7)) # 4 
# The function prototype of random.randint() is: random.randint(a, b), which is used to generate an integer in the specified range. 
#Where the parameter a is the lower limit, the parameter b is the upper limit, the generated random number n: a <= n <= b 
print (random.randrange(1,10)) # 5 
# The function prototype of random.randrange is: random.randrange ([start], stop[, step]), #Get 
a random number from the set within the specified range and incremented by the specified cardinality. Such as: random.randrange(10, 100, 2), 
#The result is equivalent to taking a random number from the sequence [10, 12, 14, 16, ... 96, 98]. 
# random.randrange(10, 100, 2) is equivalent in result to random.choice(range(10, 100, 2). 
print (random.choice( ' liukuni ' )) # i 
# random.choice from the sequence Get a random element. 
#The function prototype is: random.choice(sequence). The parameter sequence represents an ordered type. #I 
want to explain here: sequence is not a specific type in python, but a series of types in general 。
# list, tuple, and strings all belong to sequence. For sequence, you can check the data model chapter of the python manual. 
# Here are some examples of using choice: 
print (random.choice( " Learn Python " )) #Learn print (random .choice([ " JGood
" , " is " , " a " , " handsome " , " boy " ]))   # List 
print (random.choice(( " Tuple " , " List " , " Dict " )))    # List 
print (random. sample ([1,2,3,4,5],3))     # [1, 2, 5] 
# The function prototype of random.sample is: random.sample(sequence, k), randomly obtain the specified length from the specified sequence The snippet.sample function does not modify the original sequence.

practical application

# !/usr/bin/env python 
# encoding: utf-8 
import random
 import string
 #random integer: 
print ( random.randint(0,99))   # 70
 
#Randomly pick an even number between 0 and 100: 
print (random.randrange(0, 101, 2)) # 4
 
#Random floating point numbers: 
print ( random.random()) # 0.2746445568079129 
print (random.uniform(1, 10)) # 9.887001463194844
 
#Random characters: 
print (random.choice( ' abcdefg&#%^*f ' )) # f
 
#Select a specific number of characters from multiple characters: 
print (random.sample( ' abcdefghij ' ,3)) # ['f', 'h', 'd']
 
#Randomly pick a string: 
print ( random.choice ( [ ' apple ' , ' pear ' , ' peach ' , ' orange ' , ' lemon ' ] )) # apple 
# shuffle# 
items = [1,2,3, 4,5,6,7 ]
 print (items) # [1, 2, 3, 4, 5, 6, 7] 
random.shuffle(items)
 print (items) # [1, 4, 7, 2, 5, 3, 6]

verification code

import  random
random_code = '' 
for i in range(4): #The verification code requires 4 digits, so loop 4 times 
    j= random.randrange(0,4) #Because it loops 4 times, it is also 4 times here. If it is looped 5 times, Here is random.randrange(0,5) 
    if j == i:
        tmp = chr(random.randint(65,90))
    else:
        tmp = random.randint(0,9)
    random_code += str(tmp) #This is the first time I see this usage, a string += str single character forms a string 
print (random_code)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324596343&siteId=291194637