我如何“随机"选择对特定数字具有指定偏差的数字 [英] How do I "randomly" select numbers with a specified bias toward a particular number
问题描述
如何生成对一个数字具有指定偏差的随机数.例如,我将如何在两个数字 1 和 2 之间进行选择,其中 90% 偏向于 1.我能想到的最好的方法是...
How do I generate random numbers with a specified bias toward one number. For example, how would I pick between two numbers, 1 and 2, with a 90% bias toward 1. The best I can come up with is...
import random
print random.choice([1, 1, 1, 1, 1, 1, 1, 1, 1, 2])
有没有更好的方法来做到这一点?我展示的方法适用于简单的示例,但最终我将不得不使用非常具体的偏差(例如 37.65% 偏差)进行更复杂的选择,这需要很长的列表.
Is there a better way to do this? The method I showed works in simple examples but eventually I'll have to do more complicated selections with biases that are very specific (such as 37.65% bias) which would require a very long list.
我应该补充一点,我被困在 numpy 1.6 上,所以我不能使用 numpy.random.choice.
I should have added that I'm stuck on numpy 1.6 so I can't use numpy.random.choice.
推荐答案
np.random.choice
有一个 p
参数,你可以用它来指定选择的概率:
np.random.choice
has a p
parameter which you can use to specify the probability of the choices:
np.random.choice([1,2], p=[0.9, 0.1])
这篇关于我如何“随机"选择对特定数字具有指定偏差的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!