Skip to content

Instantly share code, notes, and snippets.

@Maikuolan
Last active November 19, 2016 16:12
Show Gist options
  • Save Maikuolan/b713a4b493d6958ec808f7211c79c6b8 to your computer and use it in GitHub Desktop.
Save Maikuolan/b713a4b493d6958ec808f7211c79c6b8 to your computer and use it in GitHub Desktop.
Just for laughs (Re: PHP Group PHP predictions discussion).
<?php
/* If [1] Paul the Octopus can successfully predict the winners for the 2010
* FIFA World Cup 11 out of 13 times, perhaps, just for laughs, we can give it
* a go in PHP. ;-)
*
* The three next closest up-coming general elections in the world at the
* moment, as far as I could determine via googling, seem to be in [2] Ghana
* (7 December 2016, with John Dramani Mahama vs Nana Akufo-Addo), [3]
* Macedonia (11 December 2016, with Nikola Gruevski vs Zoran Zaev), and [3]
* Romania (11 December 2016, with Liviu Dragnea vs Alina Gorghiu vs Călin
* Popescu-Tăriceanu). Let's see how PHP will predict the results via using [5]
* mt_rand(), and we can compare the results of the predictions after the
* elections (pro-tip: I anticipate a very poor success rate).
*
* References:
* - [1] https://en.wikipedia.org/wiki/Paul_the_Octopus
* - [2] https://en.wikipedia.org/wiki/Ghanaian_general_election,_2016
* - [3] https://en.wikipedia.org/wiki/Macedonian_parliamentary_election,_2016
* - [4] https://en.wikipedia.org/wiki/Romanian_legislative_election,_2016
* - [5] http://php.net/mt_rand
*
* (I just wrote this for fun, just now).
*
* Offsets: Based on percentile of results from the previous election (where
* available) or popular opinion polls (where previous elections results aren't
* available).
*/
$TestParams = array(
'Ghana' => array(array('John Dramani Mahama' => 0, 'Offset' => 1.4133), array('Nana Akufo-Addo' => 0, 'Offset' => 1.5333)),
'Macedonia' => array(array('Nikola Gruevski' => 0, 'Offset' => 1.43), array('Zoran Zaev' => 0, 'Offset' => 1.253)),
'Romania' => array(array('Liviu Dragnea' => 0, 'Offset' => 1.3352), array('Alina Gorghiu' => 0, 'Offset' => 1.2841), array('Popescu-Tăriceanu' => 0, 'Offset' => 1.0))
);
$Elections = count($TestParams);
for ($ElectionNumber = 0; $ElectionNumber < $Elections; $ElectionNumber++) {
$Country = key($TestParams);
next($TestParams);
echo $Country . ' election winner: ';
$Candidates = count($TestParams[$Country]);
$TotalVotes = 0;
for ($CandidateNumber = 0; $CandidateNumber < $Candidates; $CandidateNumber++) {
$Candidate = key($TestParams[$Country][$CandidateNumber]);
for ($Iterate = 0; $Iterate < 100000; $Iterate++) {
$ThisCycle = mt_rand(0, 100) * $TestParams[$Country][$CandidateNumber]['Offset'];
$TotalVotes += $ThisCycle;
$TestParams[$Country][$CandidateNumber][$Candidate] += $ThisCycle;
}
}
usort($TestParams[$Country], function() {
$Results = func_get_args();
$Candidates = count($Results);
$A = key($Results[0]);
$B = key($Results[1]);
if ($Results[0][$A] === $Results[1][$B]) {
return 0;
}
return ($Results[0][$A] < $Results[1][$B]) ? 1 : -1;
});
reset($TestParams[$Country]);
$WinnerNumber = key($TestParams[$Country]);
$WinnerName = key($TestParams[$Country][$WinnerNumber]);
$VotePercent = round(($TestParams[$Country][$WinnerNumber][$WinnerName] / $TotalVotes) * 100, 2);
echo $WinnerName . ', by ' . $VotePercent . "% of the vote!\n";
}
/*
* Results:
*
* Ghana election winner: Nana Akufo-Addo, by 52.14% of the vote!
* Macedonia election winner: Nikola Gruevski, by 53.13% of the vote!
* Romania election winner: Liviu Dragnea, by 36.82% of the vote!
*
* BTW, if I actually somehow manage to get all three correct, you guys all owe cookies.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment