The cube algorithm will not give an even distribution over the sphere - in particular the areas near the projections of the corners will have the densest distribution of points and near the centers of the faces of the cubes will be the lowest.
You can understand this intuitively since the volume of cube projected onto the underlying sphere is larger near the corners that near the centers of the cubes faces. In fact, the volume of a small piece (that projects on a small circle on the sphere) is proportional to the
cube of size of the vector from the origin through the center of the small circle to the point on the sphere that it intersects.
So the relative volume on a cube face center (like (1,0,0)) is 1, but for a corner (e.g., (1,1,1)) is cube of sqrt(3) or 1.73 cubed, about 5.2, so almost 5 times denser!
The spreadPoints() function might do a better job, but I'm not sure.
There are a couple of errors in you JavaScript - the use of the pow(..,-1) function instead of acos(), mix ups on the angles and missing the Math object for the random() call.,
Here is similar but correct JavaScript to do what the Wolfram links says:
/*
Returns a random point of a sphere, evenly distributed over the sphere.
The sphere is centered at (x0,y0,z0) with the passed in radius.
The returned point is returned as a three element array [x,y,z].
*/
function randomSpherePoint(x0,y0,z0,radius){
var u = Math.random();
var v = Math.random();
var theta = 2 * Math.PI * u;
var phi = Math.acos(2 * v - 1);
var x = x0 + (radius * Math.sin(phi) * Math.cos(theta));
var y = y0 + (radius * Math.sin(phi) * Math.sin(theta));
var z = z0 + (radius * Math.cos(phi));
return [x,y,z];
}
sqrt(x^2+y^2+z^2)
(withoutx0
/y0
/z0
offsets) is equal to radius +/- a small tolerance for floating-point errors. – Dorris