Last active
January 10, 2021 12:56
-
-
Save znagadeon/b7cf2dc6e394e839e6cb87cc728ae2ad to your computer and use it in GitHub Desktop.
주흔 사용량 시뮬레이터
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 설정값 | |
*/ | |
const maxSuccess = 8; // 시도 가능 횟수 | |
const cost = { | |
strengthen: 500, // 강화 비용 | |
recover: 2000, // 순줌 | |
reset: 5000, // 이노 | |
}; | |
const probability = { | |
strengthen: 0.3, // 강화 확률 | |
recover: 0.05, // 순줌 | |
reset: 0.3, // 이노 | |
}; | |
const count = 10000; // 시행횟수: 커질수록 정확해지고 오래 걸림 | |
function calculateCost(cost, probability, maxFailure) { | |
let totalCost = 0; | |
let success = 0; | |
let failure = 0; | |
while (success < maxSuccess) { | |
if (failure >= maxFailure) { | |
while (Math.random() > probability.reset) { | |
totalCost += cost.reset; | |
} | |
success = 0; | |
failure = 0; | |
} else if (success + failure === maxSuccess) { | |
while (failure > 0) { | |
while (Math.random() > probability.recover) { | |
totalCost += cost.recover; | |
} | |
failure--; | |
} | |
} else { | |
totalCost += cost.strengthen; | |
if (Math.random() > probability.strengthen) { | |
failure++; | |
} else { | |
success++; | |
} | |
} | |
} | |
return totalCost; | |
} | |
function getAverage(cost, probability, maxFailure, count) { | |
let sum = 0; | |
for (let i=0; i<count; i++) { | |
sum += calculateCost(cost, probability, maxFailure); | |
} | |
return sum / count; | |
} | |
const result = {}; | |
const formatter = new Intl.NumberFormat('KR'); | |
for (i = 0; i<maxSuccess; i++) { | |
const key = i+1 === maxSuccess ? '순줌만 사용' : `${i+1}회 실패 시 이노 사용`; | |
result[key] = `평균 ${formatter.format(getAverage(cost, probability, i+1, count))}개`; | |
} | |
console.log(`시행 횟수: 각 케이스마다 ${count}번`); | |
console.table(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment