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 7 of 7
  1. #1
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    649 lotto with php

    Below is a simple php code I am working on to randomly select numbers for the 649 lotto plus an extra number for the bonus number from 1-49. I am stuck on how to set it up so that it doesn't generate duplicate numbers. All 7 seven numbers must be between 1 and 49 and not the same.

    Thanks

    PHP Code:
    <?php

        $n1 
    rand (149);
        
    $n2 rand (149);
        
    $n3 rand (149);
        
    $n4 rand (149);
        
    $n5 rand (149);
        
    $n6 rand (149);
        
    $n7 rand (149);

    echo 
    "649 plus <font color=\"#ff0000\">bonus</font> number";
    echo 
    "<br/>$n1-$n2-$n3-$n4-$n5-$n6<font color=\"#ff0000\"> $n7</font>";

    ?>
    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    for($i=1;$i<=100;$i ++){
        
    $array_numbers[]=rand (149);
        
    $array_uniquenumbers array_unique($array_numbers);
        if (
    count($array_uniquenumbers) >= 7){
            break;
        }
    }

    // if required, tyou can then build the 7 variables
    for($i=1;$i<=7;$i ++){
       ${
    'n' $i} =$array_uniquenumbers[$i];  //${'n' . $i}  or ${n . $i} or ${n$i} -->  i always need to test it to get the right for for this kind of,variable variablenames  

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Thanks raf...

    Thats totally different than I expected. I have to read up more on "array"

    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #4
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    // Assign Numbers
    for ($i=1;$i<=49;$i++) {
       
    $array[$i] = $i;
    }
    // Get Randoms
    $final_numbers = array();
    for (
    $i=1$i<=7;$i++) {
        
    $index rand(150 $i);
        
    array_push($final_numbers$array[$index]);
        unset(
    $array[$index]);
    }
    // convert to string, and display
    $string implode($final_numbers"-");
    echo 
    "<P>$string</P>"
    i modified the code used to display prime numbers from 2-(a really big number). not sure it's much shorter than the other, but it does include the implode() function to "build" a string. when i press refresh, i always get the same results, on my code and raf's. i'm not sure why...
    Last edited by dswimboy; 04-08-2004 at 05:43 PM.
    "There is more than one way to do it."

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey raf, how're ya?!

    You're doing a loop for 100 times, how can you be sure that after 100 excecutions, you have 7 unique numbers?!

    What about this:
    PHP Code:
    $uniqueNumbers = array();
    while ( 
    count($uniqueNumbers) != )
    {
        
    $temp rand(149);
        if ( !
    in_array$temp$uniqueNumbers ) )
            
    $uniqueNumbers[] = $temp;


  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey ComfusedOfLife. Hmm yes, that code of mine could be more efficient. I didn't realy think it through. Yours will be more secure and eficient
    I just set the limit to 100 because i'm fairly confident that there will be 7 unique numbers by then. but you can just as well set it to 5000 since the loop will end after seven elements are added to the array.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    confused on life has the best of our code, i think. good thinking
    "There is more than one way to do it."


  •  

    Posting Permissions

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