mercredi 23 septembre 2015

Getting random numbers between two points in PHP

I am trying to do a rather tedious task to me in my opinion because i am new to php and its frameworks. I am stuck trying to calculate a distribution point between two points. After that is done, i wanted to get any random number between those points and then add it back to the given input. So for example, If the user enters 89.56 for the lattitude base and -22.4 for the long base, and assume 5 is the distance for both points. I would like to first calculate the midpoint between the lattitude and long base. (I did that in my code already ). Next i will like to get a random number between the midpoint and distance(i did that already but i think there is an issue with it). And lastly i want to output the random number between the distance and midpoint and then add that value to $peter in my code below. So my problem lies in two parts:generating the random number and also getting the random number to be outputted. Here is my code down below:

                  <!DOCTYPE html>
              <head> 

         <title> Distance </title>
      </head>

   <body>

 <?php


  function distance($lat,$lng,$d){

      if($d != 0)
    {
   $distannce = ($lat - $lng) / 50.732;
   }
   else
   {
   $distannce = 0;
    }

    return $distannce;

    }  

     function generaterandom($t,$e){
    $min = $t;
    $max = $e;

   $star = rand($min,$max);

}

  function uniform($g,$s,$r){ //
 // %g is already the distance.. Find a way to get the lat and long and then get the midpoint then do the math

     $clap = getmidpoint($s,$r);

     $zone = generaterandom($g,$clap);


     $ta = $g - $s;  

     $la = $g + $s;

     $ga =  1 /($la - $ta ); 




 $sub = ($ga)*($zone);

 return $sub;
  }

  function getmidpoint($x,$y){
  $midpoint = ($x + $y)/2;

  }

$stand =       distance(isset($_POST['val']),isset($_POST['value']),isset($_POST['dist']));


   if (isset($_POST['dtMAX'])&& isset($_POST['dtLatLng_Lat'])) { // if the max radio button is selected and latitude is selected do math

$peter = uniform($stand,$_POST['val'], $_POST['value']);

   }

  else if (isset($_POST['dtMAX'])&& isset($_POST['dtLatLng'])) { // if max radio buttion is selected and longitude do math

   $peter = uniform($stand,$_POST['value'],$_POST['val']);

 }

    //This statement below says if everything is fine do all the functions and print


    if(isset($_POST['submit'])) {
    if(!empty($peter)){

    if (isset($_POST['dtMAX'])&& isset($_POST['dtLatLng_Lat'])){

    echo ($peter + $_POST['val']); 
    //echo out your final result

   }
    else if (isset($_POST['dtSTD'])&& isset($_POST['dtLatLng'])){

     echo ($peter + $_POST['value']); 

  }

     }
    else {
     echo "You need to check your input";   // if there is nothing in the input output this statement
      }

    }

?>
  <form method = "post">



  <input type="checkbox" name="dtLatLng_Lat"  checked="checked"  />
   <label for="dtLatLng_Lat"> Lat </label>&nbsp &nbsp &nbsp;
   base
   <input type="text"name="val"  size ="6"   style="width:60px"; /> 

    dist
   <input type="text"name="dis" id="dis" size = "1"; /> 
    <label for="dis">  </label>
    <br>

  <input type="checkbox" name="dtLatLng" id="dtLatLng" checked="checked" />
  <label for="dtLatLng"> Long </label>&nbsp &nbsp;
  base
   <input type="text" name="value" id="dtvalue" size ="6" style="width:60px";/> 
    <label for="dtvalue"> </label>&nbsp;

  dist
<input type="text"name="dist" id="dtval" size ="1"; /> 

  <br>
   Options:         
<input type="radio" name="dtMAX" id="dtMAX" />
<label for="dtMAX"> Maxium </label>



 <br>
  <input type="submit" name="submit" value="Submit" />

   </form>
  </body>
    </html>


I created an output statement to test to see if nothing has been inputted and each time whenever i enter some value, the statement is outputted " check your input" can someone guide me in the right direction. Well an explanation as to what i am doing wrong. Thanks in advance




Aucun commentaire:

Enregistrer un commentaire