그들의 확률 값을 갖는 네 개의 정수가 간단한 함수가 무작위 정수를 생성한다 : | 0.41 (2) | 0.29 (3) | 0.25 4 | 1 0.05자바 스크립트 함수는 균일 확률 자바 스크립트 (또는 JQuery와)에서
어떻게 이들 4 가지 숫자를 그들의 가능성을 고려하여 생성 할 수 있습니까? 솔루션이 게시 그러나이 generate random integers with probabilities
: 주석에
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를 만들 수 (숫자와 자신의 체중/확률을 부여
이 질문은 여기에 게시 된 것과 매우 유사하다) "
내 요구 사항에 충분하지 않습니다. 확률이 10 %, 20 %, 60 %, 10 %라고 할 때 잘 작동합니다.
이 경우 필수 배포로 notRandomNumbers를 구성하는 것이 쉽고 배열 크기가 작습니다. 그러나 확률이 20.354 %, 30.254 % 등일 수있는 일반적인 경우에는 배열 크기가 상황을 정확하게 모델링하는 데 큰 도움이됩니다.
더 일반적인 문제에 대한 해결책이 있습니까?
편집 : 감사의 말 Georg, 해결 방안입니다. 여기 최종 버전은이며, 다른 사람들에게 유용 할 수 있습니다. 각 호출에서 새로운 난수를 얻으려는 추가 추가를 피하기 위해 누적 계산을 별도의 함수로 분할했습니다.
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));
}
}
예, 누적 분포 함수를 계산하고 값에 따라 간격을 사용하여 개별 임의의 숫자를 가져옵니다. –