PHP aleatorio: excluir / incluir / flotar / negativos y otros animales

I need to generate a random pairs of numbers (floats) , within a certain range . Basically those are points for [Lat,Lng] pairs (Longitude - latitude coordinates)

I thought This would be very straight forward with

<?php echo (rand(0,60).'.'.rand(0,99999999999999).' ||  '); // yes, a string is ok...?>

but it did not give me the control over how many numbers after the float point (resolution) - which I need fixed.

So the next phase was :

<?php echo (rand(0,60*pow(10,6)))/pow(10,6).' ||  '; //pow(10,$f) where $ is the resolution ?>

and this works . sort of .. sometimes it produces results like

22.212346 ||  33.134 ||  36.870757 ||  //(rare , but does happen)

but hey - coordinates are from -90 to 90 (lon) and -180 to 180 (lan) - what about the minus ?

echo (rand(0,-180*pow(10,9)))/pow(10,9).' ||  ';

that should do it .. and combining all together should give me somehow a random string like

23.0239423525 || -135.937419777

so after all this introduction - here is Are my question(s) .

  • Being the newbie that I am - am I missing something ? is there no built-in function to generate random floats with a negative to positive range in PHP ?

  • Why is the function above sometimes turns only resolution 3,4 or 5 if it is told to return 6 (i did not apply any ABS or ROUND) - is there an automatic rounding in php ? and if there is , how to avoid it ?

  • I have noticed that the "random" is not so random - the generated numbers are always more or less series between a range - close to one another . is the PHP random a simple very-very-very fast rotating counter ?

  • how do I EXCLUDE a range from this generated range ?? (or actually and array of ranges)

I know these are a lot of questions, but any help / thought would be great ! (and if the first one is answered positively, the rest can almost be ignored :-)

preguntado el 10 de marzo de 12 a las 12:03

1 Respuestas

The rand() function can take a negative min so maybe you can do this:

   $num = mt_rand(-180000000, 180000000)/1000000;
   echo number_format($num, 6);

if you want 6 places after decimal point.

In terms of excluding a range you may have to do it in two steps. First consider the ranges that you do want. Lets say you have 3 ranges from which you want to generate the random number. range 1 = -180 to -10, range 2 = 10 - 100 and range 3 = 120 - 180. Then you can generate a random number from 1 to 3 inclusive, use that to pick one range and then generate the number in that range.

respondido 10 mar '12, 12:03

Hi thanks - your code is obviously more elegant and efficiant then mine - I did not knew the number_format() - but how can I EXCLUDE a range from the random function (the other part of the question..) - Obmerk Kronen

thanks , the point it that I need to excluded SPECIFIC range from the RANDOM - and not a RANDOM from a RANDOM - just to give you an example, let´s say I need to generate world coordinates but EXCLUDE all the coordinates that are for example "North-Korea" , or "deserts" - so I have those set of coordinats how can I exclude those ranges ? using array is my only option ? the array will be huge .. (unless I can somehow describe a closed polygon in php ...) therefor a RANGE is my only option I think. but how exactly ? - Obmerk Kronen

What I am suggesting is that you may want to exclude a range but that leaves two ranges that you want to include. Or if you want to exclude two ranges then you are including 3 ranges...and so on. I know that the number of ranges may be high, if you want to exclude n ranges you are actually including n+1 ranges. Since the random value can occur across any of the n+1 ranges you need to randomly select one of them. Then randomly select the value within the selected range. - Vicente Ramdhanie

ok .. I will go to try . thanks for your time ! I just wanted to be sure there is no way in PHP random (or other function unknown to me) to exlude / include varaiables (read - ranges) from another range .. thanks again. - Obmerk Kronen

of course in your situation the answer is a lot more complex since you want two random values that must somehow correspond to a valid range. If you are looking a a random spot in a 2d world then you might want to consider some geometric algorithms that may simplify your life. - Vicente Ramdhanie

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.