javascript 生成具有概率的随机整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8877249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate random integers with probabilities
提问by Headshota
I'm a bit confused about how to generate integer values with probabilities.
我对如何生成具有概率的整数值有点困惑。
As an example, I have four integers with their probability values: 1|0.4, 2|0.3, 3|0.2, 4|0.1
例如,我有四个整数及其概率值:1|0.4, 2|0.3, 3|0.2, 4|0.1
How can I generate these four numbers taking into account their probabilities?
考虑到它们的概率,我如何生成这四个数字?
回答by Sergio Tulentsev
Here's a useful trick :-)
这是一个有用的技巧:-)
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];
}
回答by bhups
A simple naive approach can be:
一个简单的天真的方法可以是:
function getRandom(){
var num=Math.random();
if(num < 0.3) return 1; //probability 0.3
else if(num < 0.6) return 2; // probability 0.3
else if(num < 0.9) return 3; //probability 0.3
else return 4; //probability 0.1
}
回答by Rom098
More flexible solution based on @bhups answer. This uses the array of probability values (weights). The sum of 'weights' elements should equal 1.
基于@bhups 答案的更灵活的解决方案。这使用概率值(权重)数组。“权重”元素的总和应等于 1。
var weights = [0.3, 0.3, 0.3, 0.1]; // probabilities
var results = [1, 2, 3, 4]; // values to return
function getRandom () {
var num = Math.random(),
s = 0,
lastIndex = weights.length - 1;
for (var i = 0; i < lastIndex; ++i) {
s += weights[i];
if (num < s) {
return results[i];
}
}
return results[lastIndex];
};
回答by Nina Scholz
I suggest to use a continuous check of the probability and the rest of the random number.
我建议使用对概率和其余随机数的连续检查。
This function sets first the return value to the last possible index and iterates until the rest of the random value is smaller than the actual probability.
此函数首先将返回值设置为最后一个可能的索引并迭代,直到剩余的随机值小于实际概率。
The probabilities have to sum to one.
概率之和必须为 1。
function getRandomIndexByProbability(probabilities) {
var r = Math.random(),
index = probabilities.length - 1;
probabilities.some(function (probability, i) {
if (r < probability) {
index = i;
return true;
}
r -= probability;
});
return index;
}
var i,
probabilities = [0.4, 0.3, 0.2, 0.09, 0.01 ],
count = {},
index;
probabilities.forEach(function (a) { count[a] = 0; });
for (i = 0; i < 1e6; i++) {
index = getRandomIndexByProbability(probabilities);
count[probabilities[index]]++
}
console.log(count);
回答by Fran?ois Huppé
This is the solution i find the most flexible, for picking within any set of object with probabilities:
这是我发现最灵活的解决方案,用于在任何一组具有概率的对象中进行挑选:
// set of object with probabilities:
const set = {1:0.4,2:0.3,3:0.2,4:0.1};
// get probabilities sum:
var sum = 0;
for(let j in set){
sum += set[j];
}
// choose random integers:
console.log(pick_random());
function pick_random(){
var pick = Math.random()*sum;
for(let j in set){
pick -= set[j];
if(pick <= 0){
return j;
}
}
}