...

View Full Version : Resolved Lottery Script



NancyJ
03-28-2009, 04:36 PM
I'm writing a script to help me pick lottery numbers. Its supposed to come up with the most common individual numbers and most common combinations but its coming up with a strange result.

There are approx 14 million possible combinations, out of 100,000 loops I would expect most of those 100,000 to be unique combinations but what I'm actually getting (and its the same every time I run it) is that it only comes out with 5174 unique combinations (and its that exact same number every time). Roughly half the combinations come out 20 times and the other half come out 19 times. So either this is just a quirk of the random number generator or theres something wrong with my code.



for($i=0;$i<100000;$i++)
{
$comb =array(0);
$n=0;
for($j=0;$j<6;$j++)
{
while(in_array($n,$comb))
{
$n = rand(1,49);
}
$numbers[$n]++;
$comb[]=$n;
}
asort($comb);
$combinations[implode(',',$comb)]++;
}

echo count($combinations);
arsort($numbers);
arsort($combinations);
print_r($numbers);
print_r($combinations);


Update: Looks like it works fine on a real server. I blame windows.

oesxyl
03-28-2009, 05:05 PM
I'm writing a script to help me pick lottery numbers. Its supposed to come up with the most common individual numbers and most common combinations but its coming up with a strange result.

There are approx 14 million possible combinations, out of 100,000 loops I would expect most of those 100,000 to be unique combinations but what I'm actually getting (and its the same every time I run it) is that it only comes out with 5174 unique combinations (and its that exact same number every time). Roughly half the combinations come out 20 times and the other half come out 19 times. So either this is just a quirk of the random number generator or theres something wrong with my code.



for($i=0;$i<100000;$i++)
{
$comb =array(0);
$n=0;
for($j=0;$j<6;$j++)
{
while(in_array($n,$comb))
{
$n = rand(1,49);
}
$numbers[$n]++;
$comb[]=$n;
}
asort($comb);
$combinations[implode(',',$comb)]++;
}

echo count($combinations);
arsort($numbers);
arsort($combinations);
print_r($numbers);
print_r($combinations);


Update: Looks like it works fine on a real server. I blame windows.
there are few problems:
- array(0) will insert a 0 at first position of $comb.
- in while you test $n if is in $comb before you use rand to set a value, use a do-while instead

if you look to the $numbers you will see that is not a gaussian distribution but this is because of function rand.
probably you already know that you will never get 100000 combination as far as you don't force that in the same way you do in the inner loop.

I was wrong 0 is inserted because of $n = 0 not because of array(0)
best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum