...

View Full Version : 649 lotto with php



Len Whistler
04-07-2004, 06:47 AM
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

$n1 = rand (1, 49);
$n2 = rand (1, 49);
$n3 = rand (1, 49);
$n4 = rand (1, 49);
$n5 = rand (1, 49);
$n6 = rand (1, 49);
$n7 = rand (1, 49);

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

raf
04-07-2004, 01:34 PM
for($i=1;$i<=100;$i ++){
$array_numbers[]=rand (1, 49);
$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
}

Len Whistler
04-08-2004, 02:56 AM
Thanks raf...

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

Leonard Whistler
www.stubby.ca

dswimboy
04-08-2004, 07:37 AM
// Assign Numbers
for ($i=1;$i<=49;$i++) {
$array[$i] = $i;
}
// Get Randoms
$final_numbers = array();
for ($i=1; $i<=7;$i++) {
$index = rand(1, 50 - $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...

ConfusedOfLife
04-08-2004, 08:21 AM
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:


$uniqueNumbers = array();
while ( count($uniqueNumbers) != 7 )
{
$temp = rand(1, 49);
if ( !in_array( $temp, $uniqueNumbers ) )
$uniqueNumbers[] = $temp;
}

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

dswimboy
04-08-2004, 06:44 PM
confused on life has the best of our code, i think. good thinking



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum