The method described in the article does not produce a spherically symmetric distribution since each value of z is equally likely. This means that a small circle near the north pole will contain as many points as the equator. Instead the different values for z should be selected with a probability that scales like 1/r where r is the radius of that circle (i.e. r = sin(theta)).
Alternatively you can generate random x, y, and z coordinates between -1 and 1 and toss it if it falls outside of the sphere (i.e. if x^2+y^2+z^2 > 1). Then just renormalized the coordinates so that they fall on the sphere.
The benefit of this is that the method easily generalizes to arbitrary dimension.
I haven't verified it but suspect that you might be able to improve on this by generating the coordinates in order, constantly decreasing the range to ensure that the point falls within the sphere. Then, to ensure spherical symmetry, you randomly shuffle the points at the end.
For those interested in this problem, a while back I wrote a blog post listing all the well-known ways of correctly generating random points on the surface (or inside) of a d-dimensional sphere.
It includes all the frequently used ones as well as some lesser know elegant methods.
4 comments
[ 0.20 ms ] story [ 21.3 ms ] threadAlternatively you can generate random x, y, and z coordinates between -1 and 1 and toss it if it falls outside of the sphere (i.e. if x^2+y^2+z^2 > 1). Then just renormalized the coordinates so that they fall on the sphere.
The benefit of this is that the method easily generalizes to arbitrary dimension.
I haven't verified it but suspect that you might be able to improve on this by generating the coordinates in order, constantly decreasing the range to ensure that the point falls within the sphere. Then, to ensure spherical symmetry, you randomly shuffle the points at the end.
http://extremelearning.com.au/how-to-generate-uniformly-rand...