Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Lottery Script

1. ## Lottery Script

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.

PHP 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.

• Originally Posted by NancyJ
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.

PHP 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.

Edit: I was wrong 0 is inserted because of \$n = 0 not because of array(0)

best regards

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•