xycost 发表于 2018-11-7 11:30:13

Python中的random库

很多网络演化编程中都会用到生成随机数,今天找了一下随机数说明手册,整理一下:

(1)random.seed()
伪随机数生成模块。如果不提供 seed,默认使用系统时间。使用相同的 seed,可以获得完全相同的随机数序列,常用于算法改进测试。
>>>from random import *
>>>a = Random(); a.seed(1)
>>>


>>>b = Random(); b.seed(1)
>>>


(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),结果相当于从序列中获取一个随机数。

(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.</b, 则 b <= n <= a。

xycost 发表于 2018-11-7 11:32:52

随机数生成是日常工作中经常使用的功能。下面简要介绍下Python的随机数生成标准库random。
一、常用函数
(1)生成0和1之间的浮点数,random()
生成区间为[0,1)
import random
print(random.random())
输出结果:0.8092116913076974
(2)生成a和b之间的整数,randint(a,b)
生成区间为
print(random.randint(-10,10))
输出结果:9
a必须小于等于b,如果a==b,则输出a。
print(random.randint(10,10))
输出结果:10
(3)生成a和b之间的浮点数,unifrom(a,b),也是均匀分布,因为非常常用放在这里。
生成区间为
print(random.uniform(-10,10))
输出结果:-4.109021075631352
这个函数中,a可以大于b,生成的是min(a,b)和max(a,b)之间的浮点数。
(4)生成a和b之间某个范围的整数,randrange(a,b,step)
生成区间为:在范围内,从a开始(包含a),每隔step的数形成的集合。
print(random.randrange(0,10,2))
输出结果:生成之前的偶数
print(random.randrange(1,10,2))
输出结果:生成之前的奇数
二、操作序列的函数
(1)从一个序列seq中随机选取一个元素,choice(seq)
seq1 =
seq2 = ((1,2),(3,4),(5,6))
seq3 ="abcdef"
print(random.choice(seq1))
print(random.choice(seq2))
print(random.choice(seq3))
输出结果:
5
(3, 4)
a
(2)将一个列表list中的元素随机打乱,shuffle(list)
注意,shuffle函数需要修改原序列,因此输入的序列seq必须是可更改的,所以只支持list类型。shuffle有个很传神的名字:洗牌。
seq =
print(random.shuffle(seq),seq,sep='\n')
输出结果:
None

(3)从一个序列中随机取出k个元素,sample(seq,k)
seq =
print(random.sample(seq,3),seq,sep='\n')
输出结果:


注意几个方面:一是sample并不改变原序列,因此输入序列seq可以是元组、字符串、set等数据类型。二是取出k个元素是从不同位置取值的,因此如果原序列无重复元素,形成的新序列也不会存在重复元素。三是不管输入序列seq是什么类型,sample返回的是list类型,看下例。
seq = "abcdef"
print(random.sample(seq,3),seq,sep='\n')
输出结果:
['c', 'a', 'd']
abcdef
三、随机生成符合某种分布的数据
(1)均匀分布,uniform(a,b)
见第一节描述。非常常用。
(2)正态分布,normalvariate(mu,sigma)
mu:均值
sigma:标准差
mu=0,sigma=1为标准正态分布。
除了均匀分布,正态分布用的是最多的。
import random
import numpy as np
import time
st = time.clock()
N =1000000
n = []
for i in range(N):
    n.append(random.normalvariate(0,1))
print("均值=",np.mean(n))
print("标准差=",np.std(n))
print("耗时=",time.clock() - st)
输出结果:
均值= -7.42643413388e-05
标准差= 1.00049689373
耗时= 5.702438655147374
可以看出,随机生成1000000个数,这些数符合正态分布。
(3)高斯分布,gauss(mu,sigma)
就是正态分布,采用了不同的实现方式,据说运行速度更快。
import random
import numpy as np
import time
st = time.clock()
N =1000000
n = []
for i in range(N):
    n.append(random.gauss(0,1))
print("均值=",np.mean(n))
print("标准差=",np.std(n))
print("耗时=",time.clock() - st)
输出结果
均值= 0.000604319020838
标准差= 0.999634159673
耗时= 4.827287158657131
1000000个数快了不到1秒,也没快太多。
(4)还有生成三角形分布、对数分布、指数分布、β分布、伽马分布等的函数
triangular(low, high, mode)三角形分布
lognormvariate(mu, sigma)对数分布
expovariate(lambd)指数分布
gammavariate(alpha, beta)伽马分布
等等。实际工作中,这些分布比均匀分布和正态分布用的都少的多。
---------------------
作者:saltriver
来源:CSDN
原文:https://blog.csdn.net/saltriver/article/details/52270371
版权声明:本文为博主原创文章,转载请附上博文链接!
页: [1]
查看完整版本: Python中的random库