【发布时间】:2021-12-10 14:28:51
【问题描述】:
我有一个包含 n 种弹珠的数组,这是一个简化的例子:
let marbles = [
{variable: “Green marbles”, text: “Green marble”, amount: 10},
{variable: “Yellow marbles”, text: “Yellow marble”, amount: 12},
{variable: “Pink marbles”, text: “Pink marble”, amount: 8}
]
我希望使用这个数组来计算连续获得绿色、黄色或粉红色大理石而不替换它的概率(这意味着每次绘制大理石时它都是基板)。 举个例子:
in the first pick, the probability of getting a pink marble is:
8/30.
In the second pick, the probability is now:
7/29 * 8/30
In the third pick, the probability is now:
6/28 * 7/29 * 8/30
等等。 我已经设法做到这一点,以计算 with 替换的概率,如下所示:
let withReplacementArray = []
let i
let total = marbles.reduce((prev, cur)=> {return prev + cur.amount}, 0)
marbles.forEach((cur)=> {
for(i=1; i<=total; i ++) {
withReplacementArray.push({
probability: (Math.pow(cur.amount, i))/(Math.pow(total, i)),
n: i,
text: cur.text
})
}
})
简而言之,这种方法不适用于计算没有放回的概率,因为它不再像将其提高到 i 次方那么简单。 有什么建议? 或者有什么公式可以得到这个结果?
【问题讨论】: