1. Introduction to random function
The random() method returns a randomly generated real number in the range [0,1)
Note: [0,1) includes all real numbers from 0 to 1, including 0 but not including 1
2. Syntax of random() method
import random
random.random()
Note: random() cannot be accessed directly, you need to import the random module, and then call the method through the random static object
3. Explanation of the method
1. Method: random.random()
Note: used to generate a random floating point number from 0 to 1: 0 <= n < 1.0
2. Method: random.randint(1,7)
Note: The function prototype is: random.randint(a,b) is used to generate an integer in a specified range
3 new features random.randrange ()
annotation:
(1) The function prototype is: random.randrange([start],stop[,step])
(2) Get a random number from the set that increases by the specified cardinality within the specified range, such as random.randrange(10,100,2)
The result is equivalent to obtaining a random number with a step size of 2 within 10, 100, that is, obtaining a random number from the sequence [10,12,14,16,...96,98]
(3) The result of random.randrange(10,100,2) is equivalent to random.choice(range(10,100,2))
4. Method: random.choice()
annotation:
(1) The function prototype is: random.choice(sequence), the parameter sequence represents an ordered type
(2) sequence is not a specific type in python, but refers to a series of types in general
(3) list, tuple, and strings all belong to sequence
(4) Meaning: Randomly obtain one of the elements in a fixed string or tuple
example:
print(random.choice("Learn Python"))#Learn
print(random.choice(["JGood","is","a","handsome","boy"])) #List
print(random.choice(("Tuple","List","Dict"))) #List
5. Method: random.sample()
annotation:
(1) The function prototype is: random.sample(sequence,k)
(2) Meaning: Randomly obtain a fragment of a specified length from a specified sequence
example:
print(random.sample([1,2,3,4,5],3)) #[1, 2, 5]
4. Examples:
1. Random integer
print( random.randint(0,99)) #70
2. Randomly select an even number from 0 to 100
print (random.randrange (0, 101, 2)) #4
3. Random floating point numbers
print( random.random()) #0.2746445568079129
print(random.uniform(1, 10)) #9.887001463194844
4. Random characters
print(random.choice('abcdefg&#%^*f')) #f
5. Select a specific number of characters from multiple characters
print(random.sample('abcdefghij',3)) #['f', 'h', 'd']
6. Randomly select strings
print( random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )) #apple
7. Shuffle function
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]
5. Generate random verification code
1. Define a random number variable, and then fix the number of digits of the verification code. Here, the number of digits of the verification code is specified as 4 digits.
import random check_code = '' for i in range(4): check_code += str(i) print(check_code) Result: 0123
2. Since the verification code is random and cannot always be a fixed number, we use the random number generation method below.
import random check_code = '' for i in range(4): current = random.randint(1,9) check_code += str(current) print(check_code) Result: a function that generates a random 4-digit number
3. But our verification code is generally letters and numbers
import random check_code = '' for i in range(4): current = random.randrange (0,4) if current == i: #If the current random number obtained is the same as i, get a letter tmp = chr(random.randint(65,90)) #Print out the ascc corresponding to the number, and convert the number in the brackets into letters else: tmp = random.randint(0,9) #Get the number if the current random number obtained is not equal to i check_code += str(tmp) print(check_code)
Full code:
import random check_code = '' for i in range(4): current = random.randrange (0,4) if current == i: tmp = chr(random.randint(65,90)) else: tmp = random.randint(0,9) check_code += str(tmp) print(check_code)