What is wrong with my simulation of Laplace` s studying George-Louis Leclerc `s probability of randomly dropped needle of crossing tile lines?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|cite|improve this question

















  • 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. 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














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?







share|cite|improve this question

















  • 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. 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












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?







share|cite|improve this question













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?









share|cite|improve this question












share|cite|improve this question




share|cite|improve this question








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 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












  • 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. 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







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










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?






share|cite|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "69"
    ;
    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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?






    share|cite|improve this answer

























      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?






      share|cite|improve this answer























        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?






        share|cite|improve this answer













        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?







        share|cite|improve this answer













        share|cite|improve this answer



        share|cite|improve this answer











        answered Aug 6 at 3:23









        Reese

        14.3k11135




        14.3k11135






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What is the equation of a 3D cone with generalised tilt?

            Color the edges and diagonals of a regular polygon

            Relationship between determinant of matrix and determinant of adjoint?