Skip to content

Instantly share code, notes, and snippets.

@promatik
Created May 26, 2015 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save promatik/4f47d58cf290bcb95015 to your computer and use it in GitHub Desktop.
Save promatik/4f47d58cf290bcb95015 to your computer and use it in GitHub Desktop.
Guessing Game Simulator
function guessingGame(testcases, useMidPoint) {
useMidPoint = useMidPoint || false;
var correct = i = 0,
r = Math.random;
for (i=testcases;i;i--) {
var a = r(), b = r(), k = useMidPoint ? 0.5 : r();
correct += (k < a && a > b) || (k > a && a < b);
}
console.log(correct / testcases);
}
// Use a random for K, 100 testcases
guessingGame(100); // ~0.66
// Use a random for K, 1 milion testcases
guessingGame(1000000); // ~0.66
// Use 0.5 instead of random for K, 100 testcases
guessingGame(100, true); // ~0.75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment