(1)random.seed([x])
伪随机数生成模块。如果不提供 seed,默认使用系统时间。使用相同的 seed,可以获得完全相同的随机数序列,常用于算法改进测试。
>>>from random import *
>>>a = Random(); a.seed(1)
>>>[a.randint(1, 100) for i in range(20)]
[14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 44, 77, 1, 45, 73, 23, 95, 91, 4]
>>>b = Random(); b.seed(1)
>>>[b.randint(1, 100) for i in range(20)]
[14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 44, 77, 1, 45, 73, 23, 95, 91, 4]
(2)random.random
用于生成一个0到1的随机符点数: 0 <= n < 1.0
(3)random.uniform
用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a。
(4)random.randint
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
(5)random.randrange
从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。
(6)random.choice
从序列中获取一个随机元素。参数sequence表示一个有序类型。这里要说明 一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。list, tuple, 字符串都属于sequence。
(7)random.shuffle
用于将一个列表中的元素打乱。
(8)random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。
这个模块很 "变态",还支持三角、β分布、指数分布、伽马分布、高斯分布等等非常专业的随机算法。
(9)random.triangular(low, high, mode)
Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds. The low and highbounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.
(10)random.betavariate(alpha, beta)β分布
Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.
(11)random.expovariate(lambd)指数分布
Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called “lambda”, but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.
(12)random.gammavariate(alpha, beta)伽马分布
Gamma distribution. (Not the gamma function!) Conditions on the parameters are alpha > 0 and beta > 0.
(13)random.gauss(mu, sigma)高斯分布
Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function defined below.
(14)random.lognormvariate(mu, sigma)对数正态分布
Log normal distribution. If you take the natural logarithm of this distribution, you’ll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
(15)random.normalvariate(mu, sigma)正态分布
Normal distribution. mu is the mean, and sigma is the standard deviation.
random.vonmisesvariate(mu, kappa)
mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi.
(16)random.paretovariate(alpha)帕累托分布
Pareto distribution. alpha is the shape parameter.
(17)random.weibullvariate(alpha, beta)
Weibull distribution. alpha is the scale parameter and beta is the shape parameter.
python中的random使用
最新推荐文章于 2024-08-21 18:31:17 发布