What is wrong with my simulation of Laplace` s studying George-Louis Leclerc `s probability of randomly dropped needle of crossing tile lines?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
In 1777 George-Louis Leclerc, Comte de Buffon says:
If we drop a needle onto a lined piece of paper, how likely is it to cross one of the lines? If the needle is shorter than the gap between the lines, the answer is 2/pi.
In 1812 Pierre-Simon Laplace then goes and says:
Then when we want to know something about a complex quantity, we can estimates its value by sampling from it.
Fast forward 206 years, I try to simulate throwing a needle on a ground in python 3.6 and try to estimate the value of pi with the following code:
import math
import random
iteration_count = 10000000
crossed = 0
needle_length = 1.0
gap_length = 2.0
for i in range(iteration_count):
drop_point = random.randint(-10, 10) / 10 # Needles center drops somewhere between -1 to +1 distance from any line
drop_degree_rad = random.randint(0, 157079632679) / 100000000000
tip = (math.sin(drop_degree_rad) * needle_length / 2) + drop_point
bottom = drop_point - (math.sin(drop_degree_rad) * needle_length / 2)
if math.fabs(tip) >= 1 or math.fabs(bottom) >= 1:
crossed += 1
print(crossed / iteration_count)
I expect to get a value close to 0.31830988618
which is 2 / pi / 2
, since my gap length is twice my needle length. However the values I am getting are close to 0.343843
, where I would derive pi to be: 2.90830408064
, which is 8% off.
Obviously I am missing something, but what?
probability statistics trigonometry random-variables sampling
add a comment |Â
up vote
0
down vote
favorite
In 1777 George-Louis Leclerc, Comte de Buffon says:
If we drop a needle onto a lined piece of paper, how likely is it to cross one of the lines? If the needle is shorter than the gap between the lines, the answer is 2/pi.
In 1812 Pierre-Simon Laplace then goes and says:
Then when we want to know something about a complex quantity, we can estimates its value by sampling from it.
Fast forward 206 years, I try to simulate throwing a needle on a ground in python 3.6 and try to estimate the value of pi with the following code:
import math
import random
iteration_count = 10000000
crossed = 0
needle_length = 1.0
gap_length = 2.0
for i in range(iteration_count):
drop_point = random.randint(-10, 10) / 10 # Needles center drops somewhere between -1 to +1 distance from any line
drop_degree_rad = random.randint(0, 157079632679) / 100000000000
tip = (math.sin(drop_degree_rad) * needle_length / 2) + drop_point
bottom = drop_point - (math.sin(drop_degree_rad) * needle_length / 2)
if math.fabs(tip) >= 1 or math.fabs(bottom) >= 1:
crossed += 1
print(crossed / iteration_count)
I expect to get a value close to 0.31830988618
which is 2 / pi / 2
, since my gap length is twice my needle length. However the values I am getting are close to 0.343843
, where I would derive pi to be: 2.90830408064
, which is 8% off.
Obviously I am missing something, but what?
probability statistics trigonometry random-variables sampling
1
If you are using Python 2, then integer division returns an integer. This might cause an error e.g. in drop_point and so on.
– Daniel Robert-Nicoud
Aug 6 at 3:03
@DanielRobert-Nicoud Edited the question, using 3.6.
– Koray Tugay
Aug 6 at 3:04
1
My first suggestion would be to userandom.uniform
just to be sure there isn't some typo you (and I) are not seeing. Wait! That's probably the problem. You're only sampling a small number of discrete points for the center of the needle. Trydrop_point=random.uniform(-1,1)
and see what happens.
– saulspatz
Aug 6 at 3:18
@saulspatz Thanks, that was it.
– Koray Tugay
Aug 6 at 3:28
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In 1777 George-Louis Leclerc, Comte de Buffon says:
If we drop a needle onto a lined piece of paper, how likely is it to cross one of the lines? If the needle is shorter than the gap between the lines, the answer is 2/pi.
In 1812 Pierre-Simon Laplace then goes and says:
Then when we want to know something about a complex quantity, we can estimates its value by sampling from it.
Fast forward 206 years, I try to simulate throwing a needle on a ground in python 3.6 and try to estimate the value of pi with the following code:
import math
import random
iteration_count = 10000000
crossed = 0
needle_length = 1.0
gap_length = 2.0
for i in range(iteration_count):
drop_point = random.randint(-10, 10) / 10 # Needles center drops somewhere between -1 to +1 distance from any line
drop_degree_rad = random.randint(0, 157079632679) / 100000000000
tip = (math.sin(drop_degree_rad) * needle_length / 2) + drop_point
bottom = drop_point - (math.sin(drop_degree_rad) * needle_length / 2)
if math.fabs(tip) >= 1 or math.fabs(bottom) >= 1:
crossed += 1
print(crossed / iteration_count)
I expect to get a value close to 0.31830988618
which is 2 / pi / 2
, since my gap length is twice my needle length. However the values I am getting are close to 0.343843
, where I would derive pi to be: 2.90830408064
, which is 8% off.
Obviously I am missing something, but what?
probability statistics trigonometry random-variables sampling
In 1777 George-Louis Leclerc, Comte de Buffon says:
If we drop a needle onto a lined piece of paper, how likely is it to cross one of the lines? If the needle is shorter than the gap between the lines, the answer is 2/pi.
In 1812 Pierre-Simon Laplace then goes and says:
Then when we want to know something about a complex quantity, we can estimates its value by sampling from it.
Fast forward 206 years, I try to simulate throwing a needle on a ground in python 3.6 and try to estimate the value of pi with the following code:
import math
import random
iteration_count = 10000000
crossed = 0
needle_length = 1.0
gap_length = 2.0
for i in range(iteration_count):
drop_point = random.randint(-10, 10) / 10 # Needles center drops somewhere between -1 to +1 distance from any line
drop_degree_rad = random.randint(0, 157079632679) / 100000000000
tip = (math.sin(drop_degree_rad) * needle_length / 2) + drop_point
bottom = drop_point - (math.sin(drop_degree_rad) * needle_length / 2)
if math.fabs(tip) >= 1 or math.fabs(bottom) >= 1:
crossed += 1
print(crossed / iteration_count)
I expect to get a value close to 0.31830988618
which is 2 / pi / 2
, since my gap length is twice my needle length. However the values I am getting are close to 0.343843
, where I would derive pi to be: 2.90830408064
, which is 8% off.
Obviously I am missing something, but what?
probability statistics trigonometry random-variables sampling
edited Aug 6 at 3:14
asked Aug 6 at 3:00


Koray Tugay
1034
1034
1
If you are using Python 2, then integer division returns an integer. This might cause an error e.g. in drop_point and so on.
– Daniel Robert-Nicoud
Aug 6 at 3:03
@DanielRobert-Nicoud Edited the question, using 3.6.
– Koray Tugay
Aug 6 at 3:04
1
My first suggestion would be to userandom.uniform
just to be sure there isn't some typo you (and I) are not seeing. Wait! That's probably the problem. You're only sampling a small number of discrete points for the center of the needle. Trydrop_point=random.uniform(-1,1)
and see what happens.
– saulspatz
Aug 6 at 3:18
@saulspatz Thanks, that was it.
– Koray Tugay
Aug 6 at 3:28
add a comment |Â
1
If you are using Python 2, then integer division returns an integer. This might cause an error e.g. in drop_point and so on.
– Daniel Robert-Nicoud
Aug 6 at 3:03
@DanielRobert-Nicoud Edited the question, using 3.6.
– Koray Tugay
Aug 6 at 3:04
1
My first suggestion would be to userandom.uniform
just to be sure there isn't some typo you (and I) are not seeing. Wait! That's probably the problem. You're only sampling a small number of discrete points for the center of the needle. Trydrop_point=random.uniform(-1,1)
and see what happens.
– saulspatz
Aug 6 at 3:18
@saulspatz Thanks, that was it.
– Koray Tugay
Aug 6 at 3:28
1
1
If you are using Python 2, then integer division returns an integer. This might cause an error e.g. in drop_point and so on.
– Daniel Robert-Nicoud
Aug 6 at 3:03
If you are using Python 2, then integer division returns an integer. This might cause an error e.g. in drop_point and so on.
– Daniel Robert-Nicoud
Aug 6 at 3:03
@DanielRobert-Nicoud Edited the question, using 3.6.
– Koray Tugay
Aug 6 at 3:04
@DanielRobert-Nicoud Edited the question, using 3.6.
– Koray Tugay
Aug 6 at 3:04
1
1
My first suggestion would be to use
random.uniform
just to be sure there isn't some typo you (and I) are not seeing. Wait! That's probably the problem. You're only sampling a small number of discrete points for the center of the needle. Try drop_point=random.uniform(-1,1)
and see what happens.– saulspatz
Aug 6 at 3:18
My first suggestion would be to use
random.uniform
just to be sure there isn't some typo you (and I) are not seeing. Wait! That's probably the problem. You're only sampling a small number of discrete points for the center of the needle. Try drop_point=random.uniform(-1,1)
and see what happens.– saulspatz
Aug 6 at 3:18
@saulspatz Thanks, that was it.
– Koray Tugay
Aug 6 at 3:28
@saulspatz Thanks, that was it.
– Koray Tugay
Aug 6 at 3:28
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Well, the obvious hypothesis is that your "needles" are always landing centered on points of the form $fracx10$. This is an issue - imagine an extreme example, where instead of using "random.randint(-10, 10) / 10" you used "random.randint(-1, 1) / 1". Then the needle crosses a line 2/3 of the time!
To get a decent approximation of $pi$, you should use a finer distribution - try selecting drop_point using "random.randint(-1000,1000) / 1000", or an even larger number. Or even better - doesn't Python have a way to select a uniform random float?
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Well, the obvious hypothesis is that your "needles" are always landing centered on points of the form $fracx10$. This is an issue - imagine an extreme example, where instead of using "random.randint(-10, 10) / 10" you used "random.randint(-1, 1) / 1". Then the needle crosses a line 2/3 of the time!
To get a decent approximation of $pi$, you should use a finer distribution - try selecting drop_point using "random.randint(-1000,1000) / 1000", or an even larger number. Or even better - doesn't Python have a way to select a uniform random float?
add a comment |Â
up vote
2
down vote
accepted
Well, the obvious hypothesis is that your "needles" are always landing centered on points of the form $fracx10$. This is an issue - imagine an extreme example, where instead of using "random.randint(-10, 10) / 10" you used "random.randint(-1, 1) / 1". Then the needle crosses a line 2/3 of the time!
To get a decent approximation of $pi$, you should use a finer distribution - try selecting drop_point using "random.randint(-1000,1000) / 1000", or an even larger number. Or even better - doesn't Python have a way to select a uniform random float?
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Well, the obvious hypothesis is that your "needles" are always landing centered on points of the form $fracx10$. This is an issue - imagine an extreme example, where instead of using "random.randint(-10, 10) / 10" you used "random.randint(-1, 1) / 1". Then the needle crosses a line 2/3 of the time!
To get a decent approximation of $pi$, you should use a finer distribution - try selecting drop_point using "random.randint(-1000,1000) / 1000", or an even larger number. Or even better - doesn't Python have a way to select a uniform random float?
Well, the obvious hypothesis is that your "needles" are always landing centered on points of the form $fracx10$. This is an issue - imagine an extreme example, where instead of using "random.randint(-10, 10) / 10" you used "random.randint(-1, 1) / 1". Then the needle crosses a line 2/3 of the time!
To get a decent approximation of $pi$, you should use a finer distribution - try selecting drop_point using "random.randint(-1000,1000) / 1000", or an even larger number. Or even better - doesn't Python have a way to select a uniform random float?
answered Aug 6 at 3:23
Reese
14.3k11135
14.3k11135
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2873553%2fwhat-is-wrong-with-my-simulation-of-laplace-s-studying-george-louis-leclerc-s%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
If you are using Python 2, then integer division returns an integer. This might cause an error e.g. in drop_point and so on.
– Daniel Robert-Nicoud
Aug 6 at 3:03
@DanielRobert-Nicoud Edited the question, using 3.6.
– Koray Tugay
Aug 6 at 3:04
1
My first suggestion would be to use
random.uniform
just to be sure there isn't some typo you (and I) are not seeing. Wait! That's probably the problem. You're only sampling a small number of discrete points for the center of the needle. Trydrop_point=random.uniform(-1,1)
and see what happens.– saulspatz
Aug 6 at 3:18
@saulspatz Thanks, that was it.
– Koray Tugay
Aug 6 at 3:28