Skip to content

Instantly share code, notes, and snippets.

@papoms
Created November 18, 2014 13:10
Show Gist options
  • Save papoms/81a32e51fe5cc2d02660 to your computer and use it in GitHub Desktop.
Save papoms/81a32e51fe5cc2d02660 to your computer and use it in GitHub Desktop.
Get Click Coordinates based on a normal distribution
<?php
// Return Random Float between min and max
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
// returns normally distributed coordinates in a given range (0 to nMax)
function getClickCoordinates(&$x, &$y, $xMax = 100, $yMax = 100){
// Polar-Method to get Normal Distributed coordinates
do {
$u = randomFloat(-1,1);
$v = randomFloat(-1,1);
$q =$u * $u + $v * $v;
} while ($q == 0 || $q >= 1);
$p = sqrt( (-10 * log($q)) / $q);
//Used to normalize numbers between -1 and 1
$boundary = 10;
// |----------------- between -1 and 1 ---------------|
$x = min(max($u * $p, -$boundary), $boundary) / $boundary * $xMax/2 + $xMax/2;
$y = min(max($v * $p, -$boundary), $boundary) / $boundary * $yMax/2 + $yMax/2;
}
// Output some demo variables for visualization
for ($i=0; $i < 10000 ; $i++) {
getClickCoordinates($x, $y, 180, 180);
echo "$x;$y\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment