random标准库主要提供了伪随机数生成函数和相关的类,同时也提供了SystemRandom类(也可以直接使用os.urandom()函数)来支持生成加密级别要求的不可再现伪随机数序列。
1、random.seed()
初始化随机数生成器。使用相同种子可以生成相同的随机数序列。例如:
>>> random.seed(5)
>>> random.random()
0.6229016948897019
>>> random.random()
0.7417869892607294
>>> random.seed(5)
>>> random.random()
0.6229016948897019
>>> random.random()
0.7417869892607294
>>> random.seed(123)
>>> random.randint(1,100)
6
>>> random.randint(1,100)
9
>>> random.seed(123)
>>> random.randint(1,100)
6
>>> random.randint(1,100)
9
>>> random.seed(321)
>>> random.randint(1,100)
28
>>> random.randint(1,100)
13
2、random.getrandbits(k)
生成具有k个二进制位的随机整数。例如:
>>> random.getrandbits(3)
0L
>>> random.getrandbits(3)
2L
>>> random.getrandbits(3)
7L
>>> random.getrandbits(3)
6L
>>> random.getrandbits(3)
2L
3、rangdom.randrange([start,]stop[,step])
返回range([start,]stop[,step])之间的随机数,等价于choice(range([start,]stop[,step]),区别在于该函数并不真的创建一个range对象。例如:
>>> random.randrange(5)
3
>>> random.randrange(5,20,3)
11
>>> random.randrange(5,20,5)
15
4、random.randint(start,end)
返回闭区间[start,end]之间的随机整数,类似于randrange(start,end+1)。例如:
>>> [random.randint(5,20) for i in range(20)]
[12, 16, 19, 17, 8, 15, 6, 13, 6, 12, 7, 7, 9, 7, 14, 20, 6, 9, 9, 7]
5、random.choice(seq)
从序列seq中随机选择一个元素并返回。例如:
>>> random.choice('abcdefg')
'a'
>>> random.choice('abcdefg')
'g'
>>> random.choice([1,2,3,4,5,6])
4
>>> random.choice([1,2,3,4,5,6])
2
>>> random.choice((1,2,3,4,5,6))
5
6、random.shuffle(seq)
将序列seq原地乱序。例如:
>>> x = list(range(20))
>>> random.shuffle(x)
>>> x
[16, 15, 3, 12, 6, 14, 1, 2, 13, 8, 4, 9, 17, 18, 11, 7, 19, 5, 10, 0]
7、random.sample(seq, k)
从序列或集合seq中随机选择k个不同的(这里并不是指元素值)元素,以列表形式返回。例如(接上面的代码):
>>> random.sample(x,3)
[18, 3, 2]
>>> random.sample(x,3)
[3, 17, 8]
>>> random.sample(x,3)
[2, 14, 7]
>>> y = [1,2,2,2,1,3]
>>> random.sample(y,3)
[2, 2, 1]
>>> random.sample(y,3)
[2, 2, 1]
>>> random.sample(y,3)
[2, 2, 3]
8、random.random()
返回左闭右开区间[0.0,1.0)之间的浮点数。
9、random.uniform(a,b)
返回介于[a,b]或[b,a]之间的随机浮点数。例如:
>>> random.uniform(3,5)
4.84352763680075
>>> random.uniform(5,3)
4.635435982260146
10、random.triangular(low,high,mode)
返回介于[low,high]之间的随机浮点数,mode用于确定数值如何分布。例如:
>>> random.triangular()
0.6041766419310899
>>> random.triangular()
0.809202355489536
>>> random.triangular()
0.39545940385391254
>>> random.triangular(mode=0.1)
0.11213068022993511
>>> random.triangular(mode=0.1)
0.14202201162618033
>>> random.triangular(mode=0.1)
0.07648650142198485
>>> random.triangular(3,5,mode=4.1)
4.0060114547695695
>>> random.triangular(3,5,mode=4.1)
3.7841619928542487
>>> random.triangular(3,5,mode=3.1)
3.594104706668854
11、random.betavariate(alpha,beta)
返回[0,1]之间的符合beta分布的随机浮点数,两个参数要求大于0。例如:
>>> random.betavariate(3,5)
0.4774740780821406
>>> random.betavariate(3,5)
0.3996755034928471
>>> random.betavariate(3,50)
0.058100787064147986
12、random.expovariate(lambd)
返回符合lambd分布的随机数。例如:
>>> [random.expovariate(3) for i in range(10)]
[0.023746839946594114, 0.4413273605121732, 0.12551353053608152, 0.013493207269662204, 0.29947366176757295, 0.05612131847508229,
0.047628127058363855, 1.3205129984044726, 0.08792536205084321, 0.09437795307155394]
>>> [random.expovariate(-3) for i in range(10)]
[-0.5840103217481932, -0.1429878665439176, -0.320509040220251, -0.3277959011141573, -0.4593551780229827, -0.25977304321413436, -
0.38937311888802556, -0.7204132540876763, -0.1232984045589699, -0.13544652703833246]
>>> [random.expovariate(-30) for i in range(10)]
[-0.032248098097416224, -0.050302433165153586, -0.014618853197399763, -0.011403100190286985, -0.108163759181155, -0.014175458942549098, -
0.017466307097120947, -0.0946695841475753, -0.051734748354947326, -0.003952288703677691]
13、random.gammavariate(alpha,beta)
返回符合gamma分布的随机数,要求两个参数大于0。例如:
>>> [random.gammavariate(3,5) for i in range(10)]
[12.22246084443096, 24.678533917988172, 5.486830306916827, 12.242217699498275, 27.744458573822325, 15.63044532881201, 10.310423266683404,
14.921246065682253, 5.3442532179846145, 28.23953804581197]
>>> [random.gammavariate(30,5) for i in range(10)]
[172.4916323997075, 213.82625932922335, 137.81402565067157, 162.89624745025762, 153.97373733808928, 131.34151959572236, 137.99405726886417,
183.36910321346838, 141.15859464845778, 138.23763089032002]
14、random.gauss(mu,sigma)
返回符合gauss分布的随机数,其中mu为平均数,sigma是标准差。例如:
>>> [random.gauss(3,2) for i in range(10)]
[1.7757284660473014, 5.077754226706221, 0.8824129559831824, 4.059287688438886, 3.222237914813805, 3.6594159482351074, 3.8908231956332036,
3.468488929507344, 3.1015041749618733, 4.7795188461395695]
>>> [random.gauss(30,2) for i in range(10)]
[31.1436036337679, 26.90501185383272, 30.43681854026509, 29.165851569198466, 30.930642293427265, 31.582678686552505, 30.744169293495503,
28.012052168706838, 29.663863892820565, 27.63818542918888]
>>> [random.gauss(30,20) for i in range(10)]
[4.453358574830325, 5.382067977081544, 46.03802800315538, 22.24850207514115, 48.16811096334578, 18.301937532127866, 41.88030952485087,
36.639818172662906, 57.02701135874143, 37.28867527579759]
15、random.lognormvariate(mu,sigma)
返回符合对数正态分布的随机数,mu可以为任意值,sigma必须大于0。例如:
>>> [random.lognormvariate(3,2) for i in range(10)]
[136.89029332157023, 500.70765648541476, 79.78703935304308, 8.32688876811877, 30.160030683008884, 48.76707958880316, 456.1243769893165,
7.878500122309458, 0.8948876344189048, 7.5364705758649]
16、random.normalvariate(mu,sigma)
返回符合正态分布的随机数,mu表示平均值,sigma表示标准差。
17、random.vonmisesvariate(mu,kappa)
返回符合von mises分布的随机数(弧度)。mu为[0,2*pi]之间的平均值,kappa表示浓度参数且比喻大于等于0。例如:
>>> random.vonmisesvariate(2,2)
1.9444347927073324
>>> random.vonmisesvariate(2,3)
2.2057918593621713
>>> random.vonmisesvariate(2,30)
2.2678252533865644
>>> random.vonmisesvariate(2,0)
4.878056242725338
18、random.paretovariate(alpha)
返回符合Pareto分布的随机数,alpha为形状参数。例如:
>>> random.paretovariate(3)
1.011188953140707
>>> random.paretovariate(3)
1.2772046588048263
>>> random.paretovariate(60)
1.0236529715195033
19、random.weibullvariate(alpha,beta)
返回符合Weibull分布的随机数,alpha表示比例参数,beta表示形状参数。
附:random模块中的Random和SystemRandom类还可以这样用。
>>> r = random.Random()
>>> r.randint(3,100)
63
>>> r.choice('abcdefg')
'a'
>>> r = random.SystemRandom()
>>> r.choice('abcdefg')
'c'
>>> r.choice('abcdefg')
'd'
>>> r.randint(3,100)
87
>>> r.gauss(3,5)
-4.09461403339122