Created
May 26, 2015 10:46
-
-
Save promatik/4f47d58cf290bcb95015 to your computer and use it in GitHub Desktop.
Guessing Game Simulator
This file contains hidden or 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
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); | |
} |
This file contains hidden or 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
// 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