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 12 of 12
  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

    lotto 649 with php

    Below is my completed php code that generates 6 random/unique numbers from 1 to 49 in asending order for the lotto 649. The output can be numbers but I added images instead. To see it in action go to www.stubby.ca and scroll to the bottom.

    I got help with it from this site, the phpfreak site, and the PHP Visual QuickPro Guide book. The Visual QuickPro Guide series of books I find to be very good.


    PHP Code:
    <?php
    echo "<h2>Lotto 6/49 <img src=\"images/lotto/logo649.gif\"></img></h2>";

    $randarray = array(); 
    for(
    $i 1$i <= 6; ) 

        unset(
    $rand); 
        
    $rand rand(149); 
        if(!
    in_array($rand$randarray)) 
        { 
            
    $randarray[] = $rand
            
    $i++; 
        } 


    sort ($randarray);

    $start "<img src=\"images/lotto/";
    $end ".jpg\" width=\"60\" height=\"134\" alt=\"Lotto 649\"></img>"

    echo 
    $start,$randarray[0],$end;
    echo 
    $start,$randarray[1],$end;
    echo 
    $start,$randarray[2],$end;
    echo 
    $start,$randarray[3],$end;
    echo 
    $start,$randarray[4],$end;
    echo 
    $start,$randarray[5],$end;

    ?>
    Leonard Whistler
    www.stubby.ca
    Last edited by Len Whistler; 04-25-2004 at 06:34 PM.
    Leonard Whistler

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lets purify
    PHP Code:
    <?php 
    echo '<h2>Lotto 6/49 <img src="images/lotto/logo649.gif"></img></h2>'

    $randarray = array();  
    while (
    $i 6)
    {
        unset(
    $rand);
        
    $rand rand(149);
        if(!
    in_array($rand$randarray))
        {
            
    $randarray[] = $rand;
            
    $i++;
        }
    }
    $start '<img src="images/lotto/"'// you missed a quote here for the attributevalue
    $end '.jpg" width="60" height="134" alt="Lotto 649"></img>';  
    sort ($randarray);
    for (
    $i=0;$i<=5;$i++){
        echo 
    $start,$randarray[$i],$end;
    }
    ?>
    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..

    I like to have more experienced php coders look at my code to tweak it a bit.


    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're welcome, but does that code work. I mean the ',' in the
    echo $start,$randarray[$i],$end;
    look strange. I suppose they should be dots, to concatenate.
    Also, the images alternative message should better contain the number, so that it's always readable. so than we and up with something like
    PHP Code:
    <?php  
    echo '<h2>Lotto 6/49 <img src="images/lotto/logo649.gif"></img></h2>';  

    $randarray = array();   
    while (
    $i 6

        unset(
    $rand); 
        
    $rand rand(149); 
        if(!
    in_array($rand$randarray)) 
        { 
            
    $randarray[] = $rand
            
    $i++; 
        } 

    sort ($randarray); 
    for (
    $i=0;$i<=5;$i++){ 
        echo 
    '<img src="images/lotto/"' $randarray[$i] . '.jpg" width="60" height="134" alt="Lotto 649 - number '$randarray[$i] .'"></img>';  

    ?>
    As an aside, you could also have drawn the images (or one big image) dynamically using PHP, instead of creating the 49 images ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    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
    You're welcome, but does that code work

    Yes...the original code I posted works perfectly, its been on my site for about 4 days now. I haven't tested your code yet, it might take me a few days to get around to it.

    I am looking into php generated images but that could be a few months down the road.

    Leonard Whistler
    www.stubby.ca
    Leonard Whistler

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    well I was gonna get smart and suggest to `purify` further

    PHP Code:
    <?
    $rand_keys 
    array_randrange149 ), 6) ;
    foreach( 
    $rand_keys as $k=>$v ){
        
    $str .='<img src="images/lotto/"' $v '.jpg" width="60" height="134" alt="Lotto 649 - number '$v .'"></img>';
    }
    echo 
    $str;
    ?>
    BUT , for some reason the value '0' seems to pop up from time to time & I dont get why ? , even doing an array_values() on the random array seems to have a '0' value in there .. ? any ideas ?

    <edit>
    Raf you can apparantly use a comma as a concatenator though whether this is a bug or feature I am not sure, it does not seem to be noted in the string operaters section of (my version of the) manual
    </edit>
    Last edited by firepages; 04-26-2004 at 03:49 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    well I was gonna get smart and suggest to `purify` further

    PHP Code:
    <?
    $rand_keys 
    array_randrange149 ), 6) ;
    foreach( 
    $rand_keys as $k=>$v ){
        
    $str .='<img src="images/lotto/"' $v '.jpg" width="60" height="134" alt="Lotto 649 - number '$v .'"></img>';
    }
    echo 
    $str;
    ?>
    BUT , for some reason the value '0' seems to pop up from time to time & I dont get why ? , even doing an array_values() on the random array seems to have a '0' value in there .. ? any ideas ?

    <edit>
    Raf you can apparantly use a comma as a concatenator though whether this is a bug or feature I am not sure, it does not seem to be noted in the string operaters section of (my version of the) manual
    </edit>
    Nice try, but i think the random numbers need to be sorted from low to high, which is why all that 'extra' code is needed (i had already played with the idea to do it all in one loop untill i saw the
    sort ($randarray);
    the reaon why you get the 0 is probably because arrays are 0-based but i don't immedeately see how it then becomet the value of the elements. I'd need a print_r for that.

    About the concatenating ",". Very strange. I wonder if this only works on variables (gonna try
    echo $var1 , 'testtest' , $var2;

    and if inside
    echo "this is part 1, and this, is part 2";
    echo "$var1 , testtest , $var2";
    what happens then, since the value is parsed, but i assume the ',' are printed, so it's not used to concatenate there

    If i'm right, then i concider it to be a bug since it will only work as an operator under certain circomstances for certain data.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    doh , I forgot to add sort($rand_keys); , I appreciate that the array keys will start from 0 , but the values should not , e.g. I dont see how $v can ever == 0 but it does ?

    Quote Originally Posted by Raf
    then i concider it to be a bug
    me too
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages
    doh , I forgot to add sort($rand_keys); , I appreciate that the array keys will start from 0 , but the values should not , e.g. I dont see how $v can ever == 0 but it does ?
    And how would you prevent to get duplicates ...

    I don't see how the 0 becomes the value either, but it's the most obvious explanation i see, so i would be looking into that direction first.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by raf
    And how would you prevent to get duplicates ...
    you can't get duplicates as array_rand grabs 6 unique random values from a unique array , adding 1 to the value gets over the '0' value issue but I still dont get why I need to?

    PHP Code:
    <?
    $rand_keys 
    array_randrange(1,49 ), 6);
    sort($rand_keys);    
    foreach( 
    $rand_keys as $k=>$v ){
        ++
    $v;
        
    $str .='<img src="images/lotto/"' $v '.jpg" width="60" height="134" alt="Lotto 649 - number '$v .'"></img>';
    }
    ?>

    <edit>ok the docs do not say for sure that array_rand grabs unique sets of numbers , but I just ran the above 1 million times and failed to get a duplicate (by checking against an array_unique()) so I can only assume that this is in fact the case.
    </edit>
    Last edited by firepages; 04-26-2004 at 11:52 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not familiar with array_rand since i hardly use randomly generated data (only for pwd's and private keys) so i looked it up and indeed didn't see anything about the returned values being unique, hence my question.

    I would run a
    $rand_keys = array_rand( range(1,2 ), 6);
    print_r ($rand_keys) ;

    and see how it handles that.

    From the manual

    "If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array. "

    Couldn't this mean that you do not get a value between 1 and 49 returned, but the key for each of these entrys --> so all your values should be between 0 and 48.
    Do you get 49's with your original code? (without the increment)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #12
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Warning: array_rand() [function.array-rand]: Second argument has to be between 1 and the number of elements in the array in E:\phpdev\www\sess1.htm on line 2

    I think that along with the earlier 1000K run, makes it (I think anyway) reasonably sure , short of crawling through the source.

    still not sure about the other issue at this point.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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