Sampling uniformly distributed random points inside a spherical volume

前端 未结 9 683
无人及你
无人及你 2020-11-27 13:10

I am looking to be able to generate a random uniform sample of particle locations that fall within a spherical volume.

The image below (courtesy of http://nojhan.fre

9条回答
  •  一个人的身影
    2020-11-27 13:36

    import random
    R = 2
    
    def sample_circle(center):
        a = random.random() * 2 * np.pi
        r = R * np.sqrt(random.random())
        x = center[0]+ (r * np.cos(a))
        y = center[1] + (r * np.sin(a))
        return x,y
    
    ps = np.array([sample_circle((0,0)) for i in range(100)])
    
    plt.plot(ps[:,0],ps[:,1],'.')
    plt.xlim(-3,3)
    plt.ylim(-3,3)
    plt.show()
    

提交回复
热议问题