我有一个文件,不同的值的一些概率,例如:

1 0.1
2 0.05
3 0.05
4 0.2
5 0.4
6 0.2

我想用这个分布生成随机数。是否存在处理此问题的现有模块?自己编写代码是相当简单的(构建累积密度函数,生成一个随机值[0,1]并选择相应的值),但这似乎应该是一个常见的问题,可能有人已经为它创建了一个函数/模块。

我需要这个,因为我想生成一个生日列表(它不遵循标准随机模块中的任何分布)。


当前回答

另一个答案,可能更快:)

distribution = [(1, 0.2), (2, 0.3), (3, 0.5)]  
# init distribution  
dlist = []  
sumchance = 0  
for value, chance in distribution:  
    sumchance += chance  
    dlist.append((value, sumchance))  
assert sumchance == 1.0 # not good assert because of float equality  

# get random value  
r = random.random()  
# for small distributions use lineair search  
if len(distribution) < 64: # don't know exact speed limit  
    for value, sumchance in dlist:  
        if r < sumchance:  
            return value  
else:  
    # else (not implemented) binary search algorithm  

其他回答

你可能想看看NumPy随机抽样分布

基于其他解决方案,您可以生成累积分布(作为整数或浮点数),然后您可以使用平分使其更快

这是一个简单的例子(我在这里使用整数)

l=[(20, 'foo'), (60, 'banana'), (10, 'monkey'), (10, 'monkey2')]
def get_cdf(l):
    ret=[]
    c=0
    for i in l: c+=i[0]; ret.append((c, i[1]))
    return ret

def get_random_item(cdf):
    return cdf[bisect.bisect_left(cdf, (random.randint(0, cdf[-1][0]),))][1]

cdf=get_cdf(l)
for i in range(100): print get_random_item(cdf),

get_cdf函数会将20、60、10、10转换为20、20+60、20+60+10、20+60+10+10

现在我们随机选择一个20+60+10+10的随机数。然后我们用二分法快速得到实际值

这里有一个更有效的方法:

只需使用'weights'数组调用以下函数(假设索引为相应的项)和no。所需样品的数量。可以很容易地修改此函数以处理有序对。

使用它们各自的概率返回采样/选择(带有替换)的索引(或项):

def resample(weights, n):
    beta = 0

    # Caveat: Assign max weight to max*2 for best results
    max_w = max(weights)*2

    # Pick an item uniformly at random, to start with
    current_item = random.randint(0,n-1)
    result = []

    for i in range(n):
        beta += random.uniform(0,max_w)

        while weights[current_item] < beta:
            beta -= weights[current_item]
            current_item = (current_item + 1) % n   # cyclic
        else:
            result.append(current_item)
    return result

关于while循环中使用的概念的简短说明。 我们从累积beta(均匀随机构造的累积值)中减少当前项的权重,并增加当前索引以找到与beta值匹配的项。

从Python 3.6开始,Python的标准库中就有了一个解决方案,即random.choices。

示例用法:让我们建立一个与OP问题中匹配的总体和权重:

>>> from random import choices
>>> population = [1, 2, 3, 4, 5, 6]
>>> weights = [0.1, 0.05, 0.05, 0.2, 0.4, 0.2]

现在choices(population, weights)生成一个样本,包含在一个长度为1的列表中:

>>> choices(population, weights)
[4]

可选的仅关键字参数k允许一次请求多个示例。这很有价值,因为有些准备工作是随机的。在生成样本之前,每次调用choice函数都要做的事情;通过一次生成多个样本,我们只需要做一次准备工作。这里我们生成一百万个样本,并使用集合。计数器来检查我们得到的分布是否与我们给出的权重大致匹配。

>>> million_samples = choices(population, weights, k=10**6)
>>> from collections import Counter
>>> Counter(million_samples)
Counter({5: 399616, 6: 200387, 4: 200117, 1: 99636, 3: 50219, 2: 50025})

scipy.stats。Rv_discrete可能是您想要的。您可以通过values参数提供您的概率。然后,您可以使用分布对象的rvs()方法来生成随机数。

正如Eugene Pakhomov在评论中指出的那样,你也可以将p关键字参数传递给numpy.random.choice(),例如:

numpy.random.choice(numpy.arange(1, 7), p=[0.1, 0.05, 0.05, 0.2, 0.4, 0.2])

如果你使用的是Python 3.6或更高版本,你可以使用标准库中的random.choices() -请参阅Mark Dickinson的回答。