Skip to content

Instantly share code, notes, and snippets.

@dmuth dmuth/solve.php
Created Feb 17, 2016

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.