对于棋盘游戏的赔率计算器,我需要计算一场战斗平均会持续多少回合.由于战斗双方都有可能失利,因此理论上一场战斗可以永远持续下go .因此,我无法遍历所有分支,但需要计算数学极限.通过使用模拟器进行验证,我发现以下函数正确地近似于剩余的平均轮数:
// LIMIT could be any number, the larger it is, the more accurate the result.
const LIMIT = 100;
// r is the number of rounds left if at least 1 of the sides hit
// x is the chance that both sides miss and the round count gets increased,
// but the battle state stays the same.
function approximateLimitForNumberOfRounds(r: number, x: number) {
let approx = r / (1 - x);
// n -> infinity
for (let n = 1; n < LIMIT; n++) {
approx += x ** n;
}
return approx;
}
如何修改此函数以准确计算剩余的轮数,而不是近似计算?(注意,由于x
是一个机会,它包含在(0, 1)
或0 < x < 1
中).