-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added intergration of Monte Carlo for PHP #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Butt4cak3
merged 10 commits into
algorithm-archivists:master
from
PudottaPommin:php_monte_carlo
Oct 7, 2018
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4bc5868
Added intergration of Monte Carlo for PHP
PudottaPommin b411169
Changed code style
PudottaPommin 559c7fc
Update monte_carlo.php
PudottaPommin 95eafaf
Update monte_carlo.php
PudottaPommin a7a3738
Update monte_carlo.php
PudottaPommin afa9d6d
Update monte_carlo.php
PudottaPommin ed52163
Update monte_carlo.php
PudottaPommin ec2f420
Update monte_carlo.php
PudottaPommin e3409c6
Merge branch 'master' into php_monte_carlo
PudottaPommin 4a241aa
Styles fixes and typo
PudottaPommin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
function in_circle(float $positionX, float $positionY, float $radius = 1): bool | ||
Butt4cak3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return pow($positionX, 2) + pow($positionY, 2) < pow($radius, 2); | ||
PudottaPommin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
function random_zero_to_one(): float | ||
{ | ||
return mt_rand() / mt_getrandmax(); | ||
} | ||
|
||
function monte_carlo(int $samples, float $radius = 1): float | ||
{ | ||
$inCircleCount = 0; | ||
|
||
for ($i = 0; $i < $samples; $i++) { | ||
if (in_circle(random_zero_to_one() * $radius, random_zero_to_one() * $radius, $radius)) { | ||
$inCircleCount++; | ||
} | ||
} | ||
|
||
return 4 * $inCircleCount / $samples; | ||
} | ||
|
||
$piEstimate = monte_carlo(10000000); | ||
$percentError = abs($piEstimate - pi()) / pi() * 100; | ||
|
||
printf('The estimate of PI is: %s', $piEstimate); | ||
echo PHP_EOL; | ||
printf('The percent error is: %s', $percentError); | ||
echo PHP_EOL; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.