5%の確率に挑戦して少なくとも1回成功する割合が85%を超えるのは、何回挑戦したときか→37回
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
const main = () => { 'use strict'; const p = console.log; const probabilityOf_percent = (percent) => { const randomDouble100 = Math.random() * 100; return (randomDouble100 < percent); }; const challengeForOneWin = (percent, times) => { for(let i=0; i<times; i++){ if(probabilityOf_percent(percent)){ return true; } } return false; }; const getOneWinProportion = (percent, times) => { let cnt = 0; for(let i=0; i<times; i++){ if(challengeForOneWin(percent, times)){ cnt++; } } const proportion = cnt * 100.0 / times; return proportion; }; const getOneWinProportionAverage_calculation = (percent, challengeTimes, times) => { let proportionSum = 0; for(let i=0; i<times; i++){ proportionSum += getOneWinProportion(percent, challengeTimes); } const proportionAverage = proportionSum / times; return proportionAverage; }; const getOneWinProportionAverage_analytic = (percent, challengeTimes) => { const result = (1.0 - Math.pow(1 - percent / 100.0, challengeTimes)) * 100; return result; }; const sub = () => { const times = 10000; const challengeTimes = 37; const percent = 5.0; const result_calculation = getOneWinProportionAverage_calculation(percent, challengeTimes, times); const result_analytic = getOneWinProportionAverage_analytic(percent, challengeTimes); p("result_calculation : " + result_calculation); p("result_analytic : " + result_analytic); }; sub(); }; main(); |
1 2 |
result_calculation : 85.03594594594543 result_analytic : 85.01097459511844 |