November 5, 201213 yr Anyone got any idea why.. My script doesn't work the way it should. It makes my laptop *really* hot, like there's an infinite loop in there or something? <?php function isOdd($num){ $num = $num % 2; return $num; } function pair($names) { foreach ($names as &$name) { // So I got the first one.. I need to pair it with a second.. $count = count($names)-1; $random = rand(1, $count); $match = $names[$random]; while ($name == $match) { $random = rand(1, $count); } if ($name != $match && $match != "") { echo "[$count] $name and $match sitting in a tree<br/>"; unset($names[$random]); unset($name); } } } $names = array("Lewis", "Steve", "Shelley", "Cal", "Danny", "Ruth", "Barry", "Nyall", "Scott", "Kerrie", "Carly", "Ray"); $count = count($names); if (isOdd($count)) { echo "You can't have an odd number of people."; } else { echo "Let's crack on!"; } pair($names); ?>
November 5, 201213 yr while ($name == $match) { $random = rand(1, $count); } This loop was the first I saw, and it looks like there isn't anything being changed to the condition. So this loop never stops ? Léon
November 5, 201213 yr Author Hmm, the point of that is, I can't have the $name = $match. I don't want to be able to draw myself when running the script. How would I go about that?
November 5, 201213 yr I think this will fix it: while ($name == $match) { $random = rand(0, $count); $match = $names[$random]; } Als you might want to use 0 instead of 1 in the random function: $random = rand(0, $count); Otherways the first name of the array is never used. Léon
Create an account or sign in to comment