Last Updated: 2023-04-23 10:02:41 Sunday
-- TOC --
randint(a,b)
随机返回一个a到b之间的int,包含边界值a和b,即[a,b]
。
>>> import random
>>> random.randint(1,3)
3
>>> random.randint(1,3)
1
>>> random.randint(1,3)
2
randrange
randrange,就是在一个range内返回一个随机的int,跟内置的range接口一样,可以设置step,并且不包括上限,因此random.randrange(0)
会raise ValueError。
>>> random.randrange(10)
8
>>> random.randrange(10)
6
>>> random.randrange(10)
0
>>> random.randrange(0,10,3)
3
>>> random.randrange(0,10,3)
0
>>> random.randrange(0,10,3)
6
>>> random.randrange(0,10,3)
0
>>> random.randrange(0,10,3)
9
choice
从一个序列中随机选一个,如果seq为空,会raise IndexError:
>>> random.choice([1,2,3])
3
>>> random.choice([1,2,3])
2
>>> random.choice('abcdefg')
'f'
choices
choices,随机选多个出来,可重复,返回是一个list。
>>> random.choices([1,2,3,4,5])
[3]
>>> random.choices([1,2,3,4,5])
[5]
>>> random.choices([1,2,3,4,5], k=2)
[5, 5]
>>> random.choices([1,2,3,4,5], k=2)
[3, 1]
>>> random.choices([1,2,3,4,5], k=2)
[2, 4]
>>> random.choices('123456', k=3)
['5', '2', '6']
>>> random.choices('123456', k=3)
['2', '1', '4']
>>> random.choices('123456', k=3)
['6', '5', '5']
以上示例,choices在每一次做选择的时候,以相同的权重对待每一个元素。
If a weights sequence is specified, selections are made according to the relative weights. Alternatively, if a cum_weights sequence is given, the selections are made according to the cumulative weights (perhaps computed using itertools.accumulate()). For example, the relative weights [10, 5, 30, 5] are equivalent to the cumulative weights [10, 15, 45, 50]. Internally, the relative weights are converted to cumulative weights before making selections, so supplying the cumulative weights saves work.
我们还可以给每个元素设置权重,权重越大,每次获得选中的概率就越大。
>>> random.choices('abcde', [10,10,50,30,10], k=3)
['c', 'a', 'd']
>>> random.choices('abcde', [10,10,50,30,10], k=3)
['c', 'b', 'e']
>>> random.choices('abcde', [10,10,50,30,10], k=3)
['e', 'd', 'c']
>>> random.choices('abcde', [10,10,50,30,10], k=3)
['a', 'e', 'c']
>>> random.choices('abcde', [10,10,50,30,10], k=3)
['c', 'a', 'd']
>>> random.choices('abcde', [10,10,50,30,10], k=3)
['c', 'c', 'c']
c的相对权重最高,每次都会有c出现的被选中列表中。
还可以用range对象作为输入,实现在一个int范围内的(可重复的)选择:
>>> random.choices(range(1000), k=6)
[923, 479, 838, 18, 769, 324]
sample(population,k)
与choices有点类似,也去随机取样,但是不重复,即不会选重复的元素。
>>> random.sample([1,2,3,4,5,6], k=3)
[4, 3, 2]
>>> random.sample([1,2,3,4,5,6], k=3)
[6, 1, 2]
我们可以用sample函数来变相实现immutable序列的shuffle:
>>> a = (1,2,3,4,5,6,7)
>>> a = tuple(random.sample(a, len(a)))
>>> a
(5, 4, 7, 3, 2, 1, 6)
不重复随机在一个范围内选择int:
>>> random.sample(range(1000), k=8)
[437, 594, 429, 169, 383, 925, 645, 939]
洗牌函数,而且是就地洗,因此只能输入mutable序列。
>>> a = [1,2,3,4,5,6,7]
>>> random.shuffle(a)
>>> a
[2, 4, 1, 3, 7, 5, 6]
random
返回一个[0,1)
区间的float:
>>> random.random()
0.4043229995771711
>>> random.random()
0.7562192317458468
>>> random.random()
0.5891326077617483
uniform(a,b)
返回一个a和b之间的float,inclusive。uniform表示均匀分布。
>>> import random
>>> random.uniform(1,2)
1.4538797541396087
>>> random.uniform(1,2)
1.390140450192471
>>> random.uniform(1,2)
1.6064227929505996
>>> random.uniform(1,2)
1.1212200451327201
>>> random.uniform(1,2)
1.4950333280619168
>>> random.uniform(10,2)
3.187152956019501
>>> random.uniform(10,2)
5.995269045377007
通过设置一个seed值,可以实现获取相同的随机序列:
>>> random.seed(123)
>>> random.randrange(100)
6
>>> random.randrange(100)
34
>>> random.randrange(100)
11
>>>
>>> random.seed(123)
>>> random.randrange(100)
6
>>> random.randrange(100)
34
>>> random.randrange(100)
11
设置seed值后,不仅随机数字序列相同,随机选择也相同:
>>> random.seed(123)
>>> random.choice((1,2,3,4,5))
1
>>> random.choice((1,2,3,4,5))
3
>>> random.choice((1,2,3,4,5))
1
>>> random.choice((1,2,3,4,5))
4
>>>
>>> random.seed(123)
>>> random.choice((1,2,3,4,5))
1
>>> random.choice((1,2,3,4,5))
3
>>> random.choice((1,2,3,4,5))
1
>>> random.choice((1,2,3,4,5))
4
设置seed值后,洗牌函数(shuffle)的结果也相同:
>>> a = [1,2,3,4,5,6,7]
>>> random.seed(9)
>>> random.shuffle(a)
>>> a
[2, 7, 1, 6, 3, 5, 4]
>>>
>>> b = [1,2,3,4,5,6,7]
>>> random.seed(9)
>>> random.shuffle(b)
>>> b
[2, 7, 1, 6, 3, 5, 4]
本文链接:https://cs.pynote.net/sf/python/202301252/
-- EOF --
-- MORE --