Javascript函数生成具有非均匀概率的随机整数 [英] Javascript function to generate random integers with nonuniform probabilities
问题描述
在javascript(或jquery)中有一个简单的函数,它有四个整数及其概率值:1 | 0.41,2 | 0.29,3 | 0.25,4 | 0.05
In javascript (or jquery) is there a simple function to have four integers with their probability values: 1|0.41, 2|0.29, 3|0.25, 4|0.05
如何根据其概率生成这四个数字?
how can I generate these four numbers taking into account their probabilities ?
此问题与此处发布的问题非常类似:生成具有概率的随机整数
This question is very similar to the one posted here: generate random integers with probabilities
然而,解决方案在那里发布:
HOWEVER the solution posted there:
function randomWithProbability() {
var notRandomNumbers = [1, 1, 1, 1, 2, 2, 2, 3, 3, 4];
var idx = Math.floor(Math.random() * notRandomNumbers.length);
return notRandomNumbers[idx];
}
评论中的状态动态创建notRandomNumbers(给定数字及其权重/概率)
states in the comment "create notRandomNumbers dynamically (given the numbers and their weight/probability)"
这不足以满足我的需求。当概率为10%,20%,60%,10%时,这很有效。
This is insufficient for my needs. That works well when the probabilities are say 10%,20%, 60%,10%.
在这种情况下,使用所需的分布构造notRandomNumbers很容易,并且数组大小很小是小。但是在一般情况下概率可能是20.354%,30.254%等,数组大小对于正确建模情况来说是巨大的。
In that case constructing notRandomNumbers with the required distribution is easy and the array size is small. But in the general case where probabilities can be something like 20.354%,30.254% etc , the array size would be huge to correctly model the situation.
是否有一个干净的解决方案这个更普遍的问题?
Is there a clean solution to this more general problem?
编辑:谢谢Georg,解决方案已接受,这是我的最终版本,这可能对其他人有用。我已将累积计算拆分为单独的函数,以避免在每次调用时额外添加以获得新的随机数。
Thanks Georg, solution accepted, here is my final version, which may be useful for others. I have split the calculation of the cumulative into a separate function in order to avoid extra additions at each call to get a new random number.
function getRandomBinFromCumulative(cumulative) {
var r = Math.random();
for (var i = 0; i < cumulative.length; i++) {
if (r <= cumulative[i])
return i;
}
}
function getCummulativeDistribution(probs) {
var cumulative = [];
var sum = probs[0];
probs.forEach(function (p) {
cumulative.push(sum);
sum += p;
});
// the next 2 lines are optional
cumulative[cumulative.length - 1] = 1; //force to 1 (if input total was <>1)
cumulative.shift(); //remove the first 0
return cumulative;
}
function testRand() {
var probs = [0.1, 0.3, 0.3, 0.3];
var c = getCummulativeDistribution(probs);
console.log(c);
for (var i = 0; i < 100; i++) {
console.log(getRandomBinFromCumulative(c));
}
}
推荐答案
累积概率并返回 current_sum> = random_number
的项目:
Just accumulate the probabilities and return an item for which current_sum >= random_number
:
probs = [0.41, 0.29, 0.25, 0.05];
function item() {
var r = Math.random(), s = 0;
for(var i = 0; i < probs.length; i++) {
s += probs[i];
if(r <= s)
return i;
}
}
// generate 100000 randoms
a = [];
c = 0;
while(c++ < 100000) {
a.push(item());
}
// test actual distibution
c = {}
a.forEach(function(x) {
c[x] = (c[x] || 0) + 1;
});
probs.forEach(function(_, x) {
document.write(x + "=" + c[x] / a.length + "<br>")
});
这篇关于Javascript函数生成具有非均匀概率的随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!