# PHP Lottery Simulator

I have a lottery simulator on my website that simulates you playing the lottery every week until your death and tells you how much you would have won or lost. It can take a custom number of balls and rewards scheme, as I made it after Camelot changed the rules for the UK national lottery to give people an idea of how the change could affect their winnings.

Here is the code that actually simulates the lottery, once given values for prizes, number of balls and cost of play:

// Setting up some variables normally given values by POST
$numbers = range(1,49);$choices = array(8,15,28,36,40,47);
$ZeroWin = 0;$OneWin = 0;
$TwoWin = 0;$ThreeWin = 10;
$FourWin = 100;$FiveWin = 1000;
$BonusWin = 50000;$Jackpot = 1000000;
$plays = 2000;$cost = 1;

//No wins yet
$ZeroWinCount = 0;$OneWinCount = 0; $TwoWinCount = 0;$ThreeWinCount = 0;
$FourWinCount = 0;$FiveWinCount = 0;$BonusWinCount = 0;$JackpotCount = 0;

//Lottery simulating
for ($p = 1;$p<=$plays;$p++) {
$hits = 0; shuffle($numbers); //Spinning the wheel
$BonusHit = False;$balls = array_slice($numbers, 0, 7); //Choosing balls for ($a = 0; $a<=5;$a++) {
for ($b = 0;$b<=5; $b++) { if ($choices["$a"] ==$balls["$b"]) {$hits++;    } } }
switch ($hits) { case 0:$ZeroWinCount++;
break;
case 1:
$OneWinCount++; break; case 2:$TwoWinCount++;
break;
case 3:
$ThreeWinCount++; break; case 4:$FourWinCount++;
break;
case 5:
for ($q = 0;$q<=5; $q++) { if ($choices["$q"] ==$balls['6']) {
$BonusHit=True; break; } } if ($BonusHit) {
$BonusWinCount++; } else {$FiveWinCount++;
}
break;
case 6:
$JackpotCount++; } } //Calculate winnings$winnings = (($ZeroWinCount *$ZeroWin) + ($OneWinCount *$OneWin) +
($TwoWinCount *$TwoWin) + ($ThreeWinCount *$ThreeWin) +
($FourWinCount *$FourWin) + ($FiveWinCount *$FiveWin) +
($BonusWinCount *$BonusWin) + ($JackpotCount *$Jackpot));

$losings =$plays * $cost;$net = $losings -$winnings;


Seven balls are chosen because in the UK we have one bonus ball, and the balls of the player are either chosen by the visitor of the site or through lucky dip.

How efficient is this code? It isn't very complicated, but as the loop can be carried out thousands of times, and has loops within it, small things can make a big difference. If you see an improvement, or an outright mistake, please let me know.

• I have a stong intuition that you are ignorant on how machine based RAND and probability work.; In which case, there is insufficient information to review your code. I strongly suggest you run statistics on the shuffle method (which operates using the RAND | array length which is NOT ENTIRELY RANDOM). This should not be used for real life lottery, as it is predictable. – THE AMAZING Apr 25 '16 at 17:06
• Since at least two people (200_success and Richard_Grant) seem to have really examined this question, I'm surprised they didn't alert about the fact that it might be not-working code! Several variables are used for their content while they've never been assigned: $Numbers,$choices, $costs, and all the $...Wins. – cFreed Apr 25 '16 at 18:49
• I did not test the code for functionality. Code review is intended for the review of working code. – THE AMAZING Apr 25 '16 at 19:31
• Yeah sorry, it works fine, a lot of the variables like $numbers and$choices are chosen by the users, and returned via Post on the actual site. I'll edit it and just give those variables appropriate values. This isn't being used for a real life lottery, and there's no money involved anywhere, it's just a sim, so I trusted the shuffle to be good enough, without knowing how it works – Qiri Apr 26 '16 at 0:59