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

Thread: Lottery Script

  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    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.
    Last edited by NancyJ; 03-28-2009 at 03:45 PM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by NancyJ View Post
    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
    •