-
-
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
Changes from 1 commit
4bc5868
b411169
559c7fc
95eafaf
a7a3738
afa9d6d
ed52163
ec2f420
e3409c6
4a241aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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(abs($radius), 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still hasn't been resolved btw. |
||
} | ||
|
||
function random_zero_to_one(): float | ||
{ | ||
return mt_rand(0, 100) / 100; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for the 100? The recomended way to get a random float is the same as C: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is. We want number in interval <0,1> so I'm getting int in range <0,100> and dividing 100 to get required number There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That results in only 101 posible values 0.000, 0.001, 0.002, ..., 0.999, 1.0, running over 101^2 times would then be meaningless, instead you should take advantage of the full range of floating point precision by dividing by mt_getrandmax() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, it's changed, thanks for explanation :) |
||
} | ||
|
||
function monte_carlo(int $samples, float $radius = 1): float | ||
{ | ||
$in_circle_count = 0; | ||
|
||
for ($i = 0; $i < $samples; $i++) { | ||
if (in_circle(random_zero_to_one(), random_zero_to_one(), $radius)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x and y position need to be multiplied by $radius for proper range of random values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haven't seen any of the other implementations use radius to multiply X or Y. So what's different in this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other implementations chose for radius to be a constant 1.0, you put radius as a function parameter which default value 1.0 |
||
$in_circle_count++; | ||
} | ||
|
||
return 4 * $in_circle_count / $samples; | ||
} | ||
|
||
$piEstimate = monte_carlo(100000000); | ||
$percentError = abs($piEstimate - pi()) / pi() * 100; | ||
echo sprintf('The estimate of PI is: %s', $piEstimate); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I don't understand what you mean by "sprintf gives easier way to extract variable" but your other arguments make sense, I was under the impression PHP would \n to the correct line ending like C/C++. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's basically from my experience, where you don't print to console, but you need to work with strings, build them, etc. You can store sprintf to variable and work with it later. printf would print to output. yes, it would but with PHP_EOL. otherwise if you specify \n it uses that, which might be problem on some platforms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You do not do that, though, so there's no reason to use it. I have to agree with @Gorzoid here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I give you this one, I'm gonna change it. But still I'm staying, that sprintf looks better 🤣 |
||
echo PHP_EOL; | ||
echo sprintf('The percent error is: %s', $percentError); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing ?> at the end, also should have 1 newline at end of file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?> is discouraged and not needed since PHP 5.3 ( I think that version, not sure if 2/3/4 ). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment about ?> was a mixup where I thought the code wasn't running because it didn't read a closing tag. It actually was running but was very slow. |
Uh oh!
There was an error while loading. Please reload this page.