...

View Full Version : lotto 649 with php



Len Whistler
04-25-2004, 06:32 PM
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
echo "<h2>Lotto 6/49 <img src=\"images/lotto/logo649.gif\"></img></h2>";

$randarray = array();
for($i = 1; $i <= 6; )
{
unset($rand);
$rand = rand(1, 49);
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

raf
04-25-2004, 06:55 PM
Lets purify


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

$randarray = array();
while ($i < 6)
{
unset($rand);
$rand = rand(1, 49);
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;
}
?>

Len Whistler
04-25-2004, 10:05 PM
Thanks raf..

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


Leonard Whistler
www.stubby.ca

raf
04-25-2004, 10:45 PM
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
echo '<h2>Lotto 6/49 <img src="images/lotto/logo649.gif"></img></h2>';

$randarray = array();
while ($i < 6)
{
unset($rand);
$rand = rand(1, 49);
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 ...

Len Whistler
04-26-2004, 01:40 AM
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

firepages
04-26-2004, 03:45 AM
well I was gonna get smart and suggest to `purify` further ;)



<?
$rand_keys = array_rand( range( 1, 49 ), 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>

raf
04-26-2004, 10:28 AM
well I was gonna get smart and suggest to `purify` further ;)



<?
$rand_keys = array_rand( range( 1, 49 ), 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.

firepages
04-26-2004, 10:41 AM
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 ?


then i concider it to be a bug

me too :D

raf
04-26-2004, 11:08 AM
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.

firepages
04-26-2004, 11:47 AM
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?



<?
$rand_keys = array_rand( range(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>

raf
04-26-2004, 12:04 PM
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)

firepages
04-26-2004, 02:36 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum