Buffon's needle simulation in python

后端 未结 6 1574
臣服心动
臣服心动 2021-01-27 18:47
import numpy as np
import matplotlib.pylab as plt

class Buffon_needle_problem:

    def __init__(self,x,y,n,m):
        self.x = x #width of the needle
        self.y =         


        
6条回答
  •  忘掉有多难
    2021-01-27 18:55

    I would say that the problem is that you are defining the alignment of the needle by a simple linear function, when in fact the effective length of the needle from its centre is defined by a sinusoidal function.

    You want to calculate the effective length of the needle (at 90° to the lines) by using a function that will calculate it from its angle.

    Something like:

    self.z.append(np.cos(np.random.uniform(-np.pi/2, np.pi/2))*self.x)
    

    This will give the cosine of a random angle between -90° and +90°, times the length of the needle.

    For reference, cos(+/-90) = 0 and cos(0) = 1, so at 90°, the needle with have effectively zero length, and at 0°, its full length.

    I have neither mathplotlib or numpy installed on this machine, so I can't see if this fixes it, but it's definitely necessary.

提交回复
热议问题