[thelist] [php] array range check

kris burford kris at midtempo.net
Fri Oct 3 04:50:38 CDT 2003


>i've a php recursive function that is generating 2 random numbers (x and y 
>positions). as each set of new random numbers are created i want to check 
>(probably through an array) whether those numbers are within a certain 
>range of previously generated co-ordinates (so that i can prevent overlap 
>or change colour/size of points where there are multiple occurrences).

still having problems with this. the current code is:

while ($image=mysql_fetch_array($result)) {

   $newleft = mt_rand(1,90);
   $newtop = mt_rand(100,400);

for ($i=0; $i<count($existingleft); $i++) {
   while (((($newleft+5) < $existingleft[$i]) && (($newleft-5) > 
$existingleft[$i])) && ((($newtop+5) < $existingtop[$i]) && (($newtop-5) > 
$existingtop[$i]))) {
  $newleft = mt_rand(1,90);
  $newtop = mt_rand(100,400);
  }
}
  $existingleft[]= $newleft;
  $existingtop[]= $newtop;

// print stuff here
}

the while statement is never true, even when all the coordinates are 
identical (and still not 100% sure that this is the best way). any ideas 
why it's not working?

tia

kris



More information about the thelist mailing list