While Lopping

try changing $ticket = sort($ticket, SORT_NUMERIC); to just: sort($ticket, SORT_NUMERIC);

Again that made no change to the outcome, frustrating

frustrating indeed… lets cut it up and have some fun…

[php]$range = range(1, 25);
print_r($range);
echo “How does range look?

”;
$ticket = array();
for ($i=1; $i <= $n; $i++) {
$lotto = shuffle($range);
print_r($lotto);
echo “Do we have proper shuffle?

”;
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
$print_r($ticket);
echo “are there 4 numbers?

”;
sort($ticket, SORT_NUMERIC);
$print_r($ticket);
echo “are there 4 numbers that are sorted?

”;
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$lotton[0]’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’)”);
//mysql_query($que);
//echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]

Right, after doing that theres certainly a problem or two.
Outcome:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15 [15] => 16 [16] => 17 [17] => 18 [18] => 19 [19] => 20 [20] => 21 [21] => 22 [22] => 23 [23] => 24 [24] => 25 ) How does range look?

1Do we have proper shuffle?

so shuffle was returning a boolean and killing the array we can fix that and continue:

[php]$lotto = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
shuffle($lotto);
print_r($lotto);
echo “Do we have proper shuffle?

”;
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
$print_r($ticket);
echo “are there 4 numbers?

”;
sort($ticket, SORT_NUMERIC);
$print_r($ticket);
echo “are there 4 numbers that are sorted?

”;
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$lotton[0]’,’$lotton[1]’,’$lotton[2]’,’$lotton[3]’)”);
//mysql_query($que);
//echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]

That still returns a similar problem:
Array ( [0] => 11 [1] => 24 [2] => 15 [3] => 6 [4] => 12 [5] => 23 [6] => 16 [7] => 3 [8] => 14 [9] => 20 [10] => 9 [11] => 8 [12] => 22 [13] => 7 [14] => 21 [15] => 10 [16] => 13 [17] => 17 [18] => 19 [19] => 18 [20] => 5 [21] => 4 [22] => 1 [23] => 25 [24] => 2 ) Do we have proper shuffle?

looks good to me, what came after that? should have looked similar to:
Array( [0] => 11 [1] => 24 [2] => 15 [3] => 6 )are there 4 numbers?

Array( [0] => 6 [1] => 11 [2] => 15 [3] => 24 )are there 4 numbers that are sorted?

if so try :
[php]$lotto = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
shuffle($lotto);
foreach ($lotto as $value) {
if (count($ticket) != 4) {
if (!in_array($value, $ticket) {
$ticket[] = $value;
}
}
}
sort($ticket, SORT_NUMERIC);
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$ticket[0]’,’$ticket[1]’,’$ticket[2]’,’$ticket[3]’)”);
//mysql_query($que);
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]

Then if we’re looking good from there remove the // infront of the mysql_query and delete the last ‘echo’

–edit: I had to fix some variable names in the query that I had forgotten to change in my notepad :slight_smile:

The outcome in the previous message is all that was there, the rest of the page was blank.

All though on using your latest code there was finally some value to it:

Ticket 1: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 2: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 3: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 4: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)
Ticket 5: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘2’,‘6’,‘9’,‘24’)

Only problem being it was the same numbers for each one.

Just because this is frustrating and I wanted to have some fun with it…

[php]$lotto = range(1, 25);
$ticket = array();
for ($i=1; $i <= $n; $i++) {
shuffle($lotto);
for ($ii=1; $ii <= $i; $ii++;) {
shuffle($lotto); // shuffle the crap out of it :wink:
}
while(count($ticket) < 4) { // add a little extra randomness
$rand = array_rand($lotto, 1)
if (!in_array($rand, $ticket)) {
$ticket[] = $rand;
}
}
sort($ticket, SORT_NUMERIC); //sort it
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$ticket[0]’,’$ticket[1]’,’$ticket[2]’,’$ticket[3]’)”);
//mysql_query($que); //insert it
echo "Ticket " . $i . ": " . $que . “
”; // test before putting in database
}[/php]

Outcome being:
Ticket 1: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘9’,‘12’,‘16’,‘19’)
Ticket 2: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘9’,‘12’,‘16’,‘19’)
Ticket 3: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘9’,‘12’,‘16’,‘19’)
Ticket 4: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘9’,‘12’,‘16’,‘19’)
Ticket 5: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘9’,‘12’,‘16’,‘19’)

Almost there lol, just need it to be generating different numbers for each ticket

Hah! Here’s a facepalm moment for ya.

Move:
$ticket = array();
down 2 lines underneath/inside the start of the first for loop and you should have a working superrandom 4 number generator. :slight_smile:

[php]$lotto = range(1, 25);
for ($i=1; $i <= $n; $i++) {
$ticket = array();
shuffle($lotto);
for ($ii=1; $ii <= $i; $ii++;) {
shuffle($lotto); // shuffle the crap out of it :wink:
}
while(count($ticket) < 4) { // add a little extra randomness
$rand = array_rand($lotto, 1)
if (!in_array($rand, $ticket)) {
$ticket[] = $rand;
}
}
sort($ticket, SORT_NUMERIC); //sort it
$que = (“INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,’$usernameone’,’$ticket[0]’,’$ticket[1]’,’$ticket[2]’,’$ticket[3]’)”);
mysql_query($que); //insert it
}[/php]

Ticket 1: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘7’,‘8’,‘12’,‘17’)
Ticket 2: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘2’,‘11’,‘16’,‘24’)
Ticket 3: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘7’,‘10’,‘12’,‘18’)
Ticket 4: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘5’,‘8’,‘17’,‘23’)
Ticket 5: INSERT INTO lotto (id,username,one,two,three,four) VALUES (’’,‘Steven’,‘1’,‘9’,‘13’,‘24’)

At long last, it comes to an end :stuck_out_tongue: Much appreciated for all your help today, Thankyou

Very welcome, if it wasn’t just a holiday weekend we would have had this much sooner. We’ll look forward to your next puzzle! :slight_smile:

And just as you say that theres one last minor problem,
$lotto = range(1, 25);

Some of them however generate the number 0, know the cause of that?

actually I do, try changing:

if (!in_array($rand, $ticket)) {
$ticket[] = $rand;
to
if (!in_array($lotto[$rand], $ticket)) {
$ticket[] = $lotto[$rand];

This whole thread has been a great example as to what happends when you don’t use certain functions regularly–you forget exactly how they work… array_rand returns a random key from the $lotto array so we weren’t actually getting values but keys on the ticket.

Oh right I see,
Has certainly helped me understand alot more.
Again that has worked and I believe the project is complete.
Thankyou again ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service