Generating Lotto / Powerball results with PHP.

In this tutorial, I will show you how to generate random lotto results using PHP. This sort of thing may come in useful if you want to emulate your own version of Powerball or Euromillions.

I’ve made the following code configurable, so feel free to play around with some of the settings:

<?php

//The number of regular lotto numbers (non-bonus / non-powerball / white ball)
$regularNumbers = 5;

//The number of bonus / powerball numbers.
$bonusNumbers = 1;

//The range for regular numbers.
$regularNumbersMin = 1;
$regularNumbersMax = 69;

//The range for bonus / powerball numbers.
$bonusNumbersMin = 1;
$bonusNumbersMax = 26;

//Usually, the bonus number can be the same as
//one of the already-chosen regular numbers.
//Set this to true if you want the bonus number
//to be completely unique.
$bonusUniqueFromRegular = false;

//An array to hold our lotto results in.
$results = array(
    'regular_nums' => array(),
    'bonus_nums' => array()
);

//Generate the results for our regular numbers.
foreach(range(1, $regularNumbers) as $i){
    $randNum = mt_rand($regularNumbersMin, $regularNumbersMax);
    while(in_array($randNum, $results['regular_nums'])){
        $randNum = mt_rand($regularNumbersMin, $regularNumbersMax);
    }
    array_push($results['regular_nums'], $randNum);
}

//Generate the results for our bonus numbers.
foreach(range(1, $bonusNumbers) as $i){
    $randNum = mt_rand($bonusNumbersMin, $bonusNumbersMax);
    while(in_array($randNum, $results['bonus_nums']) || ($bonusUniqueFromRegular && in_array($randNum, $results['regular_nums']))){
        $randNum = mt_rand($bonusNumbersMin, $bonusNumbersMax);
    }
    array_push($results['bonus_nums'], $randNum);
}

//Sort both result arrays for the sake of readability.
//Generally, lotto results are displayed from lowest to highest.
sort($results['regular_nums']);
sort($results['bonus_nums']);

//Print out our lotto results.
echo 'Results: <br>';
echo implode(", ", $results['regular_nums']);
echo '<br>';
echo 'Bonus: <br>';
echo implode(", ", $results['bonus_nums']);

As you can see, the PHP code is pretty basic.

  1. The variable $regularNumbers declares how many regular numbers will be present in our draw. Regular numbers are non-bonus numbers.
  2. $bonusNumbers is how many bonus / powerball numbers will be present in our lotto draw. Usually, a lotto draw will only contain one bonus number.
  3. We set the min and max range for regular numbers.
  4. We set the min and max range for bonus numbers. More often than not, the bonus number will have a smaller range.
  5. The $bonusUniqueFromRegular variable is a boolean value that controls whether a bonus number can be the same as a number that was already drawn in the regular pool of numbers. This is set to FALSE by default, as the bonus number in lotto draws can usually be the same as a previously-drawn regular number.
  6. We setup a results array. This array is important as we can store our results in it and it also prevents us from selecting a previously-drawn number.
  7. Finally, we generate the lotto results using PHP’s mt_rand function.
  8. We sort the results arrays, simply because it is easier to read the results when the numbers are presented in an ascending order.
  9. We print the results out onto the page.

Hopefully, you found this guide to be useful!