Created
June 12, 2013 23:00
-
-
Save cathyjf/5769869 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
/** | |
* This program calculates the average number of pokemon you would need to | |
* catch to determine the base stats of a species at level 50. | |
* | |
* Answer: 48.655998 ± 19.320596468782078 | |
* | |
* @author Cathy J. Fitzpatrick <cathy@cathyjf.com> | |
* @licence public domain | |
*/ | |
var runTrial = function() { | |
var seenMin = [false, false, false, false, false, false]; | |
var seenMax = [false, false, false, false, false, false]; | |
var trials = 0; | |
do { | |
++trials; | |
for (var i = 0; i < 6; ++i) { | |
var v = Math.floor(Math.random() * 16); | |
if (v === 0) { | |
seenMin[i] = true; | |
} else if (v === 1) { | |
seenMax[i] = true; | |
} | |
} | |
} while ((seenMin.indexOf(false) !== -1) || (seenMax.indexOf(false) !== -1)); | |
return trials; | |
}; | |
const TRIALS = 1000000; | |
var values = []; | |
var sum = 0; | |
for (var i = 0; i < TRIALS; ++i) { | |
var v = runTrial(); | |
values.push(v); | |
sum += v; | |
} | |
var mean = sum / TRIALS; | |
var stdsum = 0; | |
for (var i = 0; i < values.length; ++i) { | |
var v = values[i] - mean; | |
stdsum += v * v; | |
} | |
var stddev = Math.sqrt(stdsum / (values.length - 1)); | |
console.log('Answer: ' + mean + ' ± ' + stddev); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment