The random module in Python is used to generate random numbers.
The following specifically introduces the functions of the random module:
1.random.random()
#Used to generate a 0 to 1
Random floating point number: 0<= n <1.0
import random
random.random()
2.random.uniform(a,b)
# Is used to generate a random number of points within a specified range, one of the two parameters is the upper limit and the other is the lower limit. If a> b, the generated random number n: a <= n <= b. If a <b, then b <= n <= a.
import random
print(random.uniform(1,10))
print(random.uniform(10,1))
3.random.randint(a, b)
# Is used to generate an integer in the specified range. Among them, the parameter a is the lower limit, and the parameter b is the upper limit. The generated random number n: a <= n <= b
import random
print(random.randint(1,10))
4.random.randrange ([start], stop [, step])
#Get a random number from the set that increases by the specified base within the specified range.
random.randrange(10, 30, 2), the result is equivalent to obtaining a random number from the sequence [10, 12, 14, 16, ... 26, 28]. random.randrange(10, 30, 2) is equivalent to random.choice(range(10, 30, 2) in the result.
import random
print(random.randrange(10,30,2))
#Parameter sequence represents an ordered type. Get a random element from a sequence. Sequence is not a specific type in Python, but refers to a series of types in general. List, tuple, and string all belong to sequence.
import random
lst = [1,2,3,4,5]
print(random.shuffle(lst))
# Is used to scramble the elements in a list, that is, to arrange the elements in the list randomly.
import random
lst = ['python','C','C++','javascript']
str = ('python is ok')
print(random.choice(str1))
print(random.choice(lst))
7.random.sample(sequence, k)
# Randomly obtain fragments of the specified length from the specified sequence and arrange them randomly. The sample function does not modify the original sequence.
import random
lst = [1,2,3,4,5]
print(random.sample(lst,4))
print(lst)