Created
July 27, 2013 16:37
-
-
Save jonathanlking/6095404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
// A really cool tool for debuging | |
/* require $_SERVER['DOCUMENT_ROOT'] . '/kint/Kint.class.php'; */ | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$throws = $_POST['number']; | |
if (empty($throws)) | |
$throws = 0; | |
// Stopping stupid input | |
define("maximum", 5000); | |
define("minimum", 10); | |
define("maxumumRandomNumberValue", 1000000000); | |
define("pi", 3.14159265359); | |
$static = 0; | |
if ($static) { | |
if ($throws < minimum) { | |
echo ("$throws throws is a few little, so try " . minimum . " instead"); | |
$throws = minimum; | |
} | |
if ($throws > maximum) { | |
echo ("The maximum API limit is 10,000, therefore your request has been capped."); | |
$throws = maximum; | |
} | |
// We need two random values for every throw - x and y positions | |
$values = $throws * 2; | |
$link = "http://www.random.org/integers/?num=$values&min=1&max=1000000000&col=1&base=10&format=plain&rnd=new"; | |
$connection = curl_init(); | |
curl_setopt($connection, CURLOPT_URL, $link); | |
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($connection, CURLOPT_TIMEOUT, '5'); | |
$data = curl_exec($connection); | |
curl_close($connection); | |
// As the numbers were given as a single string spereated by line breaks, create an array of the numbers | |
$randomNumbers = explode("\n", $data); | |
// The last element will be empty as there is nothing after the final line break, so remove it | |
array_pop($randomNumbers); | |
$xPositions = array_slice($randomNumbers, 0, $throws); | |
$yPositions = array_slice($randomNumbers, $throws); | |
$inTheCircle = 0; | |
for ($i = 0; $i < $throws; $i++) { | |
$x = $xPositions[$i] / maxumumRandomNumberValue; | |
$y = $yPositions[$i] / maxumumRandomNumberValue; | |
// Use the distance formula to wark out the distance from the origin | |
$distance = sqrt(pow($x, 2) + pow($y, 2)); | |
// If the point is in the circle increase the number by one | |
if ($distance <= 1) | |
$inTheCircle++; | |
echo "x: $x y: $y distance: $distance<br>"; | |
} | |
} | |
else { | |
$inTheCircle = 0; | |
for ($i = 0; $i < $throws; $i++) { | |
$x = rand() / getrandmax(); | |
$y = rand() / getrandmax(); | |
// Maximum value is 2147483647 | |
// Use the distance formula to wark out the distance from the origin | |
$distance = sqrt(pow($x, 2) + pow($y, 2)); | |
// If the point is in the circle increase the number by one | |
if ($distance <= 1) | |
$inTheCircle++; | |
/* echo "x: $x y: $y distance: $distance<br>"; */ | |
} | |
} | |
// The number of 'arrows' in the circle * 4 should be equal to pi | |
$pi = ($inTheCircle / $throws) * 4; | |
$accuracy = (($pi/pi) -1) * 100; | |
echo ("$pi - Accurate to $accuracy % - $throws Throws"); | |
include('submit.html'); | |
} | |
else { | |
include('submit.html'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment