Sampling uniformly distributed random points inside a spherical volume

前端 未结 9 639
无人及你
无人及你 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:45

    While I prefer the discarding method for spheres, for completeness I offer the exact solution.

    In spherical coordinates, taking advantage of the sampling rule:

    phi = random(0,2pi)
    costheta = random(-1,1)
    u = random(0,1)
    
    theta = arccos( costheta )
    r = R * cuberoot( u )
    

    now you have a (r, theta, phi) group which can be transformed to (x, y, z) in the usual way

    x = r * sin( theta) * cos( phi )
    y = r * sin( theta) * sin( phi )
    z = r * cos( theta )
    

提交回复
热议问题