Skip to content

Instantly share code, notes, and snippets.

@ahy4
Last active February 13, 2018 16:19
Show Gist options
  • Save ahy4/0e3418f27927b2950045699faeb9fa89 to your computer and use it in GitHub Desktop.
Save ahy4/0e3418f27927b2950045699faeb9fa89 to your computer and use it in GitHub Desktop.
// 5勝でOKラインになり8敗で割れる ⇒ 5勝7敗までセーフ
// であるから、求める確率は
// 4勝n敗してから5勝目をおさめる確率 (n = 0, 1, ..., 7)
// = (4勝0敗の確率 + 4勝1敗の確率 + 4勝2敗の確率 + ... + 4勝7敗の確率) × 5勝目をする確率
// $= (_4C_0p^4 + _5C_1p^4(1-p)+ _6C_2p^4(1-p)^2 + \cdots + _{11}C_7p^4(1-p)^7)\cdot p$
// ただし p は勝率とする
// nPk
const permutation = (n, k) => Array.from({length: k}, (_, i) => n - i).reduce((a, b) => a * b, 1);
// nCk
const combination = (n, k) => {
const k2 = Math.min(k, n - k);
return permutation(n, k2) / permutation(k2, k2);
};
// 勝率pの人がwin勝lose敗する確率 良い命名思いつかん
const t = p => (win, lose) => combination(win + lose, win) * (p ** win) * ((1 - p) ** lose);
// 計算結果 pをまとめたほうがパフォーマンスはいいけど、こっちのほうが使いやすい
const probability = p => Array.from({length: 8}, (_, lose) => t(p)(4, lose)).reduce((a, b) => a + b) * p;
// いろんな勝率でやる
const result = {};
for (let i = 35; i <= 75; i++) {
// 適当に四捨五入
result[`${i}%`] = `${Math.round(probability(i / 100) * 1000) / 10}%`;
}
console.log(JSON.stringify(result, null, " "));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment