jmsiox
jmsiox

Reputation: 123

Permutation generator php/java Script

I have 13 football matches with a possible results of Home Win , Draw , Away win. One has to predict all 13 games for a single bet. I have been trying to generate a script that can compute all possible bet combinations, mathematically, the number of possible matches is 3^13.

So far what I have is this in PHP;

    $count  = 1;
    $total_rows     = 13;
    $total_level    = 13;
    $total_cols     = 3;
    $total_global   = 3;

    $active_rows    = 0;
    $active_cols    = 0;
    $active_levels  = 0;
    $active_global  = 0;

    $betArray   = array();

    $aciveChoice[$total_rows]   = 0;
    $globalChoice[$total_level] = 0;

    while($active_rows < $total_rows){
        while($active_cols < $total_cols){

            while($active_global < $total_global){
                while($active_levels < $total_level){
                    echo $active_rows.' - '.$active_levels.': Select:'.$active_cols.' - '.$active_global.'<br/>';
                    $active_levels++;
                }
                echo $count++.'<br /><br /><br />';
                $active_levels  = 0;
                $active_global++;                       
            }

            $active_global = 0;
            $active_cols++;
        }
        $active_cols    = 0;
        $active_rows++; 
        }   
    }

My script is not giving all possible combinations. Any ideas on how to tackle this will be appreciated.

Upvotes: 1

Views: 140

Answers (1)

Amadan
Amadan

Reputation: 198408

$matches = 13;
$outcomes = 3;
$possibilities = $outcomes ** $matches;

for ($count = 0; $count < $possibilities; $count++) {
  echo str_pad(base_convert($count, 10, 3), $matches, '0', STR_PAD_LEFT)."\n";
}

This will print a number of 13-character strings, with three different possibilities for each character. (It is up to you to decide how to allocate meanings; I'd probably use 1 for Team1 win, 2 for Team2 win, 0 for draw.)

This will fail if you make the numbers too large, since base_convert works through float, apparently. There are ways around it, but if you're exploring that many possibilities you might have other problems :P

Upvotes: 1

Related Questions