對于棋盤游戲的賠率計算器,我需要計算一場戰斗平均會持續多少輪。因為戰斗中的雙方都有失手的可能,所以理論上一場戰斗可以永遠持續下去。因此我不能遍歷所有分支,但需要計算一個數學極限。通過模擬器驗證,我發現以下函式正確地近似了剩余的平均輪數:
// 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)or中0 < x < 1)。
uj5u.com熱心網友回復:
我們可以注意到它approx具有以下值:
r / (1 - x) # I refer to this as 'a' below
a x
a x x^2
a x x^2 x^3
a x x^2 ... x^n
因此,我們可以將數學運算式簡化為:
a (the sum of x^k from k = 1 to k = n)
接下來,我們必須注意,該序列x x^2 x^3 ...形成了一個具有第一項x和公比的幾何序列x。由于x有界0 < x < 1,這將有一個限制和,即:
x x^2 x^3 ... x^inf = x/(1-x)
x = 1(這顯然在 時以及在采用的原始函式中失敗r / (1 - x),但在這種情況下,您將簡單地將總和設為無窮大,approx如果不是,則將逃逸到無窮大undefined;所以我假設x != 1在以下計算中和x = 1可以/已經單獨處理)。
現在,由于我們既有一個用于x x^2 ...無窮大的運算式,也有一個用于approx包含的運算式,所以我們可以使用這兩個事實x x^2 ...來撰寫:approx
approx = r / (1 - x) x / (1 - x)
approx = (r x) / (1 - x)
你去吧!這是您在問題中概述的邏輯的數學等價物,壓縮為單個陳述句(我認為這是正確的:))。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491904.html
標籤:javascript 数学
上一篇:將值推入陣列
