Import module
import random
Dir function to see the use of random function
dir(random)
Commonly used functions have
random.randint
random.randint(1,10)
The meaning of the statement is to produce from 1 to 10 (1 to 10 comprising a) a random number (integer type int). (Parameter is an integer to floating point otherwise it will not be an error)
random.randint (20, 10) # This statement is wrong. It must be less than or equal to the upper limit.
random.random()
random.random()
Generating a floating point number between 0 and 1, including 0 but excluding 1
random.uniform
random.uniform(a,b)
Regardless of size and whether a float
random.uniform(3.65,10.56)#可以这样
random.uniform(10.56, 3.65)#也可以这样
random.choice
random.choice(seq)
random.choice([1, 2, 3, 5, 8, 13]) #列表
random.choice('hello') #字符串
random.choice(['hello', 'world']) #字符串组成的列表
random.range()
random.range(start,stop,step)
From start to generate a stop (not including the stop), a random integer interval of the step. start, stop, step must be an integer, and start <stop.
random.sample()
random.sample(p,k)
Sequence from p, k elements randomly acquired, generates a new sequence. sample does not change the original sequence.
This module is 666, also supports triangle, β distribution, exponential distribution, gamma distribution, Gaussian random algorithm and so very professional.
random.shuffle
random.shuffle(x)
Scrambling the order of elements in the sequence x. shuffle directly change the original sequence.