2

I would like to find out what is the most effective PHP script to evenly represent all numbers in a specific combinations subset.

Lottery problem example:

  • create 10 combinations each consisting of of 6 numbers
  • from set of number (1,2,3,4,5,6,7,8,9,10,11,12)

I know that from 12 numbers I can create 924 combinations each consisting of 6 numbers.

Since I can't afford to play 924 lines - I want to pick only 10 lines which represent evenly all my selected numbers.

So in this example it would be something like:

  • 1-2-3-4-5-6
  • 7-8-9-10-11-12

    and 8 more lines

I'm trying to avoid combinations like:

  • 1-2-3-4-5-6
  • 1-2-3-4-5-7
  • 1-2-3-4-5-8

... etc. which are almost the same; I want to evenly represent each number.

Hope that makes sense.

pdolinaj
  • 939
  • 11
  • 18
  • in how many subsets do you want to split the numbers? – Ivaylo Strandjev Jan 23 '13 at 15:43
  • So you want them in a random order? 1-2-3-4-5-6 and 1-2-3-4-5-7 will both be in that combination, why don't you want them? – Andrei Cristian Prodan Jan 23 '13 at 15:44
  • @andrei: to increase the odds of matching some subset of numbers and claiming a partial prize, e.g. on a "6 of 49" lotto, matching 3 numbers would get $10, 4 numbers = $50, 5 numbers = $10k, 6 numbers = jackpot, etc... – Marc B Jan 23 '13 at 15:48
  • There are some solutions present in this thread as well http://stackoverflow.com/questions/3742506/php-array-combinations – Ifthikhan Jan 23 '13 at 15:49

2 Answers2

1

You can create a "pool" of the numbers you want to use, and randomly draw from that pool. For instance, if you want 10 combinations of 6 numbers each, that's a total of 60 numbers. But you want each of 1-12 represented evenly, so there will be 5 of each number. So start with an array containing 5 of each 1-12, and draw randomly from the array for each set of 6.

$pool = array();
for($i = 0; $i < 5; $i++)
    for($x = 1; $x <= 12; $x++)
        $pool[] = $x;

$result = array();
for($i = 0; $i < 10; $i++) {
    $set = array();
    for($x = 0; $x < 6; $x++) {
        $key = array_rand($pool);
        $set[] = $pool[$key];
        unset($pool[$key]);
    }
    $result[] = $set;
}

// $result now contains 10 sets of 6 numbers each

Demo: http://ideone.com/NpO3h4

mellamokb
  • 53,762
  • 11
  • 101
  • 131
  • Thank you guys for your comments. I'll post my solution shortly and we will compare which solutions seems to be the best. – pdolinaj Jan 28 '13 at 21:08
0
// how many numbers are in the set, starting from 1
$numbers = 12;
$set = array();
for ($i=1;$i>=$numbers;$i++)
{
    array_push($set, $i);
}

// how many numbers in the subset
$count = 6;
$subSet = array();
while ($count > 0)
{
    // get a random number from 1 to numbers in set
    $rand=rand(0,$numbers-1);
    array_push($subSet, $set[$rand]);
    $count--;
}

// $subSet now contains a combination of 6 random numbers from 1 to 12
// keep refreshing
var_dump($subSet);

There you go, with explanations, is this what you wanted?

EDIT: I just noticed you said "the most effective way". This is not the most effective (in terms of memory used) way, but it's close.

Andrei Cristian Prodan
  • 1,096
  • 3
  • 16
  • 34