Skip to content

Instantly share code, notes, and snippets.

@galvao
Created June 2, 2011 20:49
Show Gist options
  • Save galvao/1005285 to your computer and use it in GitHub Desktop.
Save galvao/1005285 to your computer and use it in GitHub Desktop.
Random draw
<?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