Buffon Needle Simulation
up vote
0
down vote
favorite
import java.lang.Math;
public class BuffonNeedle
public static void main(String args)
double drops = 100;
int hit = 0;
for(int r=1; r<=6; r++)
for(int i = 1; i <= drops; i++)
double y = Math.random() * 2;
if(Math.sin(Math.random()*Math.PI) + y >= 2.)
hit++;
System.out.println(drops/hit);
drops = drops * 10;
Why doesn't this yield Pi when the constraints are 2 inches apart and the needle is one inch?
The simulation is run 6 times: 100, 1000, 10000, 100000, 1000000, 10000000.
java math
add a comment |
up vote
0
down vote
favorite
import java.lang.Math;
public class BuffonNeedle
public static void main(String args)
double drops = 100;
int hit = 0;
for(int r=1; r<=6; r++)
for(int i = 1; i <= drops; i++)
double y = Math.random() * 2;
if(Math.sin(Math.random()*Math.PI) + y >= 2.)
hit++;
System.out.println(drops/hit);
drops = drops * 10;
Why doesn't this yield Pi when the constraints are 2 inches apart and the needle is one inch?
The simulation is run 6 times: 100, 1000, 10000, 100000, 1000000, 10000000.
java math
You did not resethit
in the outer loop.
– Henry
Nov 12 at 6:27
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
import java.lang.Math;
public class BuffonNeedle
public static void main(String args)
double drops = 100;
int hit = 0;
for(int r=1; r<=6; r++)
for(int i = 1; i <= drops; i++)
double y = Math.random() * 2;
if(Math.sin(Math.random()*Math.PI) + y >= 2.)
hit++;
System.out.println(drops/hit);
drops = drops * 10;
Why doesn't this yield Pi when the constraints are 2 inches apart and the needle is one inch?
The simulation is run 6 times: 100, 1000, 10000, 100000, 1000000, 10000000.
java math
import java.lang.Math;
public class BuffonNeedle
public static void main(String args)
double drops = 100;
int hit = 0;
for(int r=1; r<=6; r++)
for(int i = 1; i <= drops; i++)
double y = Math.random() * 2;
if(Math.sin(Math.random()*Math.PI) + y >= 2.)
hit++;
System.out.println(drops/hit);
drops = drops * 10;
Why doesn't this yield Pi when the constraints are 2 inches apart and the needle is one inch?
The simulation is run 6 times: 100, 1000, 10000, 100000, 1000000, 10000000.
java math
java math
edited Nov 12 at 7:38
Zico
1,70211420
1,70211420
asked Nov 12 at 6:21
Kyle
33
33
You did not resethit
in the outer loop.
– Henry
Nov 12 at 6:27
add a comment |
You did not resethit
in the outer loop.
– Henry
Nov 12 at 6:27
You did not reset
hit
in the outer loop.– Henry
Nov 12 at 6:27
You did not reset
hit
in the outer loop.– Henry
Nov 12 at 6:27
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You need to reset hit
System.out.println(drops/hit);
drops = drops * 10;
hit = 0; //<------reset
If you didn't reset hit, the final result (the result whenr = 6
) will equal to run only inner loop once withdrops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around~2.8
, and the only 1 loop version is~3.14
. Do you think I'm missing something?
– Hai Hoang
Nov 12 at 6:57
Basically, if you don't reset the value ofhit
, it will ultimately alterSystem.out.println(drops/hit);
and as a result you will end up having a smaller value
– suvojit_007
Nov 12 at 7:07
Ah, I got it. Whenr = 6
, we only getdrops = 10000000
.hits
are total forr = 1,..,6
butdrops
are not.
– Hai Hoang
Nov 12 at 7:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256831%2fbuffon-needle-simulation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You need to reset hit
System.out.println(drops/hit);
drops = drops * 10;
hit = 0; //<------reset
If you didn't reset hit, the final result (the result whenr = 6
) will equal to run only inner loop once withdrops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around~2.8
, and the only 1 loop version is~3.14
. Do you think I'm missing something?
– Hai Hoang
Nov 12 at 6:57
Basically, if you don't reset the value ofhit
, it will ultimately alterSystem.out.println(drops/hit);
and as a result you will end up having a smaller value
– suvojit_007
Nov 12 at 7:07
Ah, I got it. Whenr = 6
, we only getdrops = 10000000
.hits
are total forr = 1,..,6
butdrops
are not.
– Hai Hoang
Nov 12 at 7:17
add a comment |
up vote
0
down vote
accepted
You need to reset hit
System.out.println(drops/hit);
drops = drops * 10;
hit = 0; //<------reset
If you didn't reset hit, the final result (the result whenr = 6
) will equal to run only inner loop once withdrops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around~2.8
, and the only 1 loop version is~3.14
. Do you think I'm missing something?
– Hai Hoang
Nov 12 at 6:57
Basically, if you don't reset the value ofhit
, it will ultimately alterSystem.out.println(drops/hit);
and as a result you will end up having a smaller value
– suvojit_007
Nov 12 at 7:07
Ah, I got it. Whenr = 6
, we only getdrops = 10000000
.hits
are total forr = 1,..,6
butdrops
are not.
– Hai Hoang
Nov 12 at 7:17
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You need to reset hit
System.out.println(drops/hit);
drops = drops * 10;
hit = 0; //<------reset
You need to reset hit
System.out.println(drops/hit);
drops = drops * 10;
hit = 0; //<------reset
answered Nov 12 at 6:33
suvojit_007
1,3151517
1,3151517
If you didn't reset hit, the final result (the result whenr = 6
) will equal to run only inner loop once withdrops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around~2.8
, and the only 1 loop version is~3.14
. Do you think I'm missing something?
– Hai Hoang
Nov 12 at 6:57
Basically, if you don't reset the value ofhit
, it will ultimately alterSystem.out.println(drops/hit);
and as a result you will end up having a smaller value
– suvojit_007
Nov 12 at 7:07
Ah, I got it. Whenr = 6
, we only getdrops = 10000000
.hits
are total forr = 1,..,6
butdrops
are not.
– Hai Hoang
Nov 12 at 7:17
add a comment |
If you didn't reset hit, the final result (the result whenr = 6
) will equal to run only inner loop once withdrops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around~2.8
, and the only 1 loop version is~3.14
. Do you think I'm missing something?
– Hai Hoang
Nov 12 at 6:57
Basically, if you don't reset the value ofhit
, it will ultimately alterSystem.out.println(drops/hit);
and as a result you will end up having a smaller value
– suvojit_007
Nov 12 at 7:07
Ah, I got it. Whenr = 6
, we only getdrops = 10000000
.hits
are total forr = 1,..,6
butdrops
are not.
– Hai Hoang
Nov 12 at 7:17
If you didn't reset hit, the final result (the result when
r = 6
) will equal to run only inner loop once with drops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around ~2.8
, and the only 1 loop version is ~3.14
. Do you think I'm missing something?– Hai Hoang
Nov 12 at 6:57
If you didn't reset hit, the final result (the result when
r = 6
) will equal to run only inner loop once with drops = 100 + 1000 + 10000 + 100000 +1000000 + 10000000
. But when I test it with online Java tool, the OP's version 6th result is around ~2.8
, and the only 1 loop version is ~3.14
. Do you think I'm missing something?– Hai Hoang
Nov 12 at 6:57
Basically, if you don't reset the value of
hit
, it will ultimately alter System.out.println(drops/hit);
and as a result you will end up having a smaller value– suvojit_007
Nov 12 at 7:07
Basically, if you don't reset the value of
hit
, it will ultimately alter System.out.println(drops/hit);
and as a result you will end up having a smaller value– suvojit_007
Nov 12 at 7:07
Ah, I got it. When
r = 6
, we only get drops = 10000000
. hits
are total for r = 1,..,6
but drops
are not.– Hai Hoang
Nov 12 at 7:17
Ah, I got it. When
r = 6
, we only get drops = 10000000
. hits
are total for r = 1,..,6
but drops
are not.– Hai Hoang
Nov 12 at 7:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256831%2fbuffon-needle-simulation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You did not reset
hit
in the outer loop.– Henry
Nov 12 at 6:27