You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中二项分布实现错误排查请求:三个概率计算函数的数学与代码正确性校验

Need help verifying probability formula implementations in JavaScript

Hey everyone, I'm working on implementing three probability calculations in JavaScript and need a second pair of eyes to check both the math logic and code correctness. Right now, all three functions are returning inconsistent results, and I can't track down the bug in probability_three—it's giving 18.00% instead of the expected 9.75%.

Background & Formulas

I'm implementing three probability formulas related to dice rolls:

  1. Single outcome probability:
    P(d, n) = 1 - ((d - 1)/d)^n
  2. Range of outcomes probability:
    P(d, n, o) = 1 - ((d - (d - o + 1))/d)^n
  3. Binomial probability (exact k successes):
    P(d, n, o, k) = nCk * (p^k) * q^(n−k)
    Where:
    • nCk = combination of n things taken k at a time
    • p = probability of a single success
    • q = 1 - p

Combination (nCk) Implementation

I based the combination function on standard factorial calculations:

function factorialize(num) { 
 // Step 1. Create a variable result to store num 
 var result = num; 
 // If num = 0 OR num = 1, the factorial will return 1 
 if (num === 0 || num === 1) return 1; 
 // Instantiate the while loop 
 while (num > 1) { 
 // Decrement by 1 
 num-- 
 // Update the result 
 result *= num 
 } 
 // Return 
 return result; 
} 
function nCr(n, r){ 
 // Compute n factorial 
 var nFact = factorialize(n) 
 // Compute r factorial 
 var rFact = factorialize(r) 
 // Compute n - r factorial 
 var nrFact = factorialize(n - r) 
 // Compute nCr 
 var result = (nFact / (rFact * nrFact)) 
 // Return 
 return result 
}

Function Implementations & Issues

1. probability_one (returns 9.75% as expected)

This calculates the probability of rolling at least one specific value across n dice with d sides:

function probability_one() { 
 // Get number of dice 
 var n = document.getElementById("n1").value 
 // Get sides on each die 
 var d = document.getElementById("d1").value 
 // Calculate 
 var prob = 1 - ((d - 1) / d)**n 
 prob = parseFloat(prob*100).toFixed(2)+"%" 
 prob = `<B>${prob}</B>` 
 // Print the probability 
 var p = document.createElement('p') 
 p.innerHTML = prob 
 document.getElementById("output1").appendChild(p) 
}

2. probability_two (returns 9.75% as expected)

This calculates the probability of rolling at least one value in a range (defined by o) across n dice with d sides:

function probability_two() { 
 // Get number of dice 
 var n = document.getElementById("n2").value 
 // Get sides on each die 
 var d = document.getElementById("d2").value 
 // Get the specific outcome 
 var o = document.getElementById("o2").value 
 // Calculate 
 var prob = 1 - ((d - (d - o + 1)) / d)**n 
 prob = parseFloat(prob*100).toFixed(2)+"%" 
 prob = `<B>${prob}</B>` 
 // Print the probability 
 var p = document.createElement('p') 
 p.innerHTML = prob 
 document.getElementById("output2").appendChild(p) 
}

3. probability_three (returns 18.00% instead of expected 9.75%)

This should calculate the binomial probability of getting exactly k successes (where a success is rolling a value that meets the o threshold) across n dice with d sides. I've added console logs to debug, but can't pinpoint the issue:

function probability_three(){ 
 // USER INPUTS 
 // Get number of dice 
 var n = document.getElementById("n3").value 
 // Get sides on each die 
 var d = document.getElementById("d3").value 
 // Get the value that defines a success 
 var o = document.getElementById("o3").value 
 // Get the number of success needed 
 var k = document.getElementById("k3").value 
 // CALCULATIONS 
 // p 
 var p = ((d - o) + 1)/10 
 console.log(`p: ${p}`) 
 // q 
 var q = (1 - p) 
 console.log(`q: ${q}`) 
 // nCr 
 var vnCr = nCr(n, k) 
 console.log(`nCr: ${vnCr}`) 
 // p**k 
 var pk = p**k 
 console.log(`p**k: ${pk}`) 
 // q**n-k 
 var pnk = q**(n-k) 
 console.log(`q**n-k: ${pnk}`) 
 // Probability 
 var prob = (vnCr * pk * pnk) / (p + q)**n 
 console.log(`Prob.: ${prob}`) 
 prob = parseFloat(prob*100).toFixed(2)+"%" 
 prob = `<B>${prob}</B>` 
 // Print the probability 
 var p = document.createElement('p') 
 p.innerHTML = prob 
 document.getElementById("output3").appendChild(p) 
}

I'd appreciate any help spotting where I went wrong with probability_three—either in translating the math to code or in the underlying logic. Let me know if you need more details about input values or expected behavior!

内容的提问来源于stack exchange,提问作者Mikitz06

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 17:38:14