[Numpy-discussion] Even Sphere Volume

Stéfan van der Walt stefan@sun.ac...
Sun Jul 5 15:50:40 CDT 2009

2009/7/5 Ian Mallett <geometrian@gmail.com>:
> I'm trying to get a cloud particle system working.  As part of determining
> the depth of every point (to get the light that can pass through), it makes
> sense that the volume should be of even density.  The volume is a sphere.
> Currently, I'm using:
> vecs = numpy.random.standard_normal(size=(size,size,3))
> magnitudes = numpy.sqrt((vecs*vecs).sum(axis=-1))
> uvecs = vecs / magnitudes[...,numpy.newaxis]
> randlen = numpy.random.random((size,size))
> randpoints = uvecs*randlen[...,numpy.newaxis]
> The particles' positions are set to randpoints.  However, this creates only
> an even distribution of directions on the sphere.  The particles are too
> closely centered around the center.  What I want is a certain number of
> particles arranged evenly throughout the sphere.  How do I do that?

I can think of two ways:

Uniformly generate points in a cube, and throw away all those outside
the sphere or, if you want to generate them directly, draw from a
uniformly distribution and transform the coordinates appropriately.
If you want more info on the latter approach, let me know.


More information about the NumPy-Discussion mailing list