Created
June 2, 2011 20:49
-
-
Save galvao/1005285 to your computer and use it in GitHub Desktop.
Random draw
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
<?php | |
/** | |
* Random draw, "interactively" reducing the original array so each participant is drawn only once. | |
* @author Er Galvão Abbott <galvao@galvao.eti.br> | |
*/ | |
$rounds = 3; | |
$eagerParticipants = array('Ad Rock', 'Mike D', 'MCA', 'Chuck D', 'Flavor Flav', 'Jammaster Jay', 'Terminator X'); | |
$luckyBastards = array(); | |
do { | |
shuffle($eagerParticipants); | |
$rand = mt_rand(0, (count($eagerParticipants) - 1)); | |
$luckyBastards[] = $eagerParticipants[$rand]; | |
$eagerParticipants = array_diff($eagerParticipants, $luckyBastards); | |
$rounds--; | |
} while ($rounds > 0); | |
echo "\nWinners:\n" . implode("\n", $luckyBastards); | |
echo "\n\nWeiners:\n" . implode("\n", $eagerParticipants); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment