This module implements various distributed pseudo-random number generators. You can draw a random number in the interval, you can draw an element from the list, you can draw a sample from the distribution.
The random module cannot be accessed directly, you need to import the random module, and then call this method through the random static object.
import random
1 Generate random numbers
Random generator seed, under the same seed, generate the same random number
random.seed()
Returns the next random floating point number in the range [0, 1.0).
random.random()
random.int(a,b) generates an integer, a <= N <= b.
random.randint(1,4) #
2
2 Randomly select one in the sequence
Random select an integer in the sequence range() random.randrange( start, stop, step)
random.randrange(1,5,2)
3
random.choice(population), returns a random element from the non-empty sequence population. If seq is empty, an IndexError is raised.
random.choice([2,5,8])
8
random.choices(population, weights=None, *, cum_weights=None, k). Extract K from the whole according to the weight, and the length of the weight sequence must be equal to the length of the population. Obviously, no weight is given and k=1. It is equivalent to
random.choice(population).
random.choices(['red', 'black', 'green'], [4, 10, 2], k=6)
['green', 'black', 'red', 'black', 'green', 'black']
random.sample(population, k, *, counts=None), returns a k-length list of unique elements selected from the population sequence or set, that is, random sampling without repetition (no replacement).
random.sample(['red', 'black', 'green'], 2)
['red', 'green']
After version 3.9, the counts parameter is added, and the number can be specified. random.sample(['red','red','red','black','green'], 2) is equivalent to random.sample([ 'red','black','green'],counts=[3,1,1], 2)
3 draw a sample from the distribution
The commonly used distributions are as follows:
function | distributed |
---|---|
gauss(mu,sigma)/normalvarite(mu,sigma) | Normal distribution |
uniform(a,b) | Evenly distributed |
expovariate(lamda) | index distribution |
gammavariate(alpha,beta) | Gamma distribution |
lognormvariate(mu,sigma) | Lognormal distribution |
betavariate (alpha, beta) | Beta distribution |
weibullvariate(alpha,beta) | Weibull distribution |
paretovariate(alpha) | Pareto distribution |
vonmisesvariate (mu, kappa) | von Mises distribution ( |
random.gauss(1,2)
-0.34372692554557815
random.uniform(1,2)
1.7719936383153665
random.expovariate(1)
0.079384517694696
random.gammavariate(1,3)
3.8134444290373866
random.lognormvariate(1,2)
2.561312589509173
random.betavariate(1,2)
0.03565784800319274
random.weibullvariate(1,2)
1.211306128111137
random.paretovariate(1)
1.709941711311189
random.vonmisesvariate(1,2)
2.377632588889548
data = [random.gammavariate(2,4) for i in range(10000)] # 抽取10000个gamma分布样本
#直方图
import matplotlib.pyplot as plt
plt.hist(data, bins=100, color="#FF0000", alpha=.7)
4 Shuffle the order
random.shuffle(x [, random]) Shuffle the sequence x randomly.
a=[1,3,5,7,9]
random.shuffle(a);print(a)
[3, 7, 5, 1, 9] # 原地打乱顺序