All combinations in lottery game in PHP

Ever wondered how a computer program can generate the file with all combinations in number lottery game? Did you think that this would involve hundreds or more lines of code? The case is much simpler than you think! For example, we will draw 6 numbers from the set of 49.
We should first consider how we would like to solve this problem. The first thing that comes to mind is what will be the first and last combination. The first will be: 1,2,3,4,5,6 the last 44,45,46,47,48,49. This gives us a starting point to determine the algorithm. The next combinations:
1,2,3,4,5,7
1,2,3,4,5,8

.

.

.

1,2,3,4,5,49. At this point, the penultimate number we increase by 1 and impose a condition that the last number must be at the beginning by 1 higher than the penultimate one:

1,2,3,4,6,7
1,2,3,4,6,8

1,2,3,4,6,49. And again we added the 1:

1,2,3,4,7,8

1,2,3,4,7,49

Suppose that we are somewhere in the middle:

10,11,12,47,48,49. We can not in any way change the last 3 numbers. We increase third from the left by 1:
10,11,13,47,48,49. In the loop must be condition imposed that the third from the left must be at most equal to a third from the right minus 1:
10,11,46,47,48,49 etc.

Become a Professionalist in Web Application Development! Enroll In The Course Of Laravel You will also learn how to create hybrid applications for smartphones

Thoughts above are used in the following script. It consists of five nested do...while(); loops. Perhaps this is not the only and best solution to this problem.

<?php
/**
 * Generating all combinations of 6 numbers out of 49
 * 
 *  @author     NetProgs.pl <www.netprogs.pl>
 *  @copyright  (c) 2014 NetProgs.pl
 *  @license  Distributed under the MIT License.
 */
$tabela = array('liczba1' => '', 'liczba2' => '', 'liczba3' => '', 'liczba4' => '', 'liczba5' => '', 'liczba6' => '');
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$e = 5;
$f = 49;

$p = fopen('kombinacje6-49.txt', 'a');

do {
    do {
        do {
            do {
                do {
                    $tabela['liczba1'] = $a;
                    $tabela['liczba2'] = $b;
                    $tabela['liczba3'] = $c;
                    $tabela['liczba4'] = $d;
                    $tabela['liczba5'] = $e;

                    for ($v = $e + 1; $v <= $f; $v++)
                    {
                        $tabela['liczba6'] = $v;
                        $plik = implode(',', $tabela);
                        fwrite($p, $plik . "\n");
                    }
                    $e++;
                } while ($tabela['liczba5'] !== 48);

                $d++;
                $e = $d + 1;
            } while ($tabela['liczba4'] !== 47);

            $c++;
            $d = $c + 1;
            $e = $d + 1;
        } while ($tabela['liczba3'] !== 46);

        $b++;
        $c = $b + 1;
        $d = $c + 1;
        $e = $d + 1;
    } while ($tabela['liczba2'] !== 45);

    $a++;
    $b = $a + 1;
    $c = $b + 1;
    $d = $c + 1;
    $e = $d + 1;
} while ($tabela['liczba1'] !== 44);

fclose($p);
?>

The script on the average computer is executed out almost 10 minutes. The server on which you perform the script may be set to a maximum time of execution php script smaller than required. Then you have to increase it.

It is not recommend the open file with Windows notepad! (Because it may not open), Notepad ++ is recommended. The file weighs more than 200 MB.

The correct solution we can see by opening the generated file.

The first three sixes:

The last three sixes:

We see that the last saved line number is 13,983,816. Exactly so are the combinations of 6 numbers from the set of 49 numbers (no repeats).

And finally: remember if you win thanks to this share winnings with the author 🙂 To be more serious: if we have a file with all combinations of Lottery game will it increase our chances for the jackpot? Unfortunately not. Writing such a script is only good exercise of algorithmic thinking. Presented part of the program can be considered as a model for use in programming languages which are more suited to this task than PHP (such as C ++ or Java).

enroll in the course of Laravel framework >>>> or affiliate program
Share

Leave a Reply

Your email address will not be published. Required fields are marked *