Created
February 17, 2016 04:37
-
-
Save dmuth/55a68cad19c1af01d114 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
#!/usr/bin/env php | |
<?php | |
/** | |
* This script solves the problem posted at https://www.facebook.com/hserus/posts/10209365831889209 | |
* | |
* If you choose an answer to this question at random, what is the chance you will be correct? | |
* | |
* A) 25% | |
* B) 50% | |
* C) 60% | |
* D) 25% | |
* | |
*/ | |
/** | |
* Run the odds, and see if we're correct or not. | |
*/ | |
function run($odds) { | |
$retval = array(); | |
$correct = false; | |
// | |
// Pick a random question, and get that percentage chance. | |
// | |
$index = mt_rand(0, 3); | |
$chance = $odds[$index]; | |
// | |
// Now create a random percentage, and if it's less than the chance, | |
// we're correct! | |
// | |
$roll = mt_rand(0, 99); | |
if ($roll < $chance) { | |
$correct = true; | |
} | |
$retval["index"] = $index; | |
$retval["letter"] = chr(65 + $index); | |
$retval["chance"] = $chance; | |
$retval["roll"] = $roll; | |
$retval["correct"] = $correct; | |
return($retval); | |
} // End of run() | |
/** | |
* Our main entry point. | |
*/ | |
function main($num_guesses) { | |
// | |
// Our group of odds by percentage | |
// | |
$odds = array(25, 50, 60, 25); | |
$num_correct = 0; | |
for ($i=0; $i<$num_guesses; $i++) { | |
$results = run($odds); | |
//printf("Guess: %s, Chance: %d, Roll: %d, Correct?: %s\n", | |
// $results["letter"], $results["chance"], $results["roll"], $results["correct"]); | |
if ($results["correct"]) { | |
$num_correct++; | |
} | |
} | |
printf("Correct rate: %d / %d\n", $num_correct, $num_guesses); | |
printf("Chance of a correct answer: %.2f%%\n", ( $num_correct / $num_guesses ) * 100 ); | |
} // End of main() | |
main(1000000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment