jacobolus 21 hours ago

Here are a few alternatives:

https://observablehq.com/@jrus/stereorandom

At least when trying to end up with stereographically projected coordinates, in general it seems to be faster to uniformly generate a point in the disk by rejection sampling and then transform it by a radially symmetric function to lie on the sphere, rather than uniformly generating a point in the ball and then projecting outward. For one thing, fewer of the points get rejected because the disk fills more of the square than the ball fills of the cube.

arvindh-manian a day ago

> The advantage of this approach is that it generalizes efficiently to any number of dimensions.

I am unsure about whether this is true. The ratio of a ball’s volume to its enclosing hypercube’s volume should decrease to 0 as dimensionality increases. Thus, the approach should actually generalize very poorly.

  • alberto_balsam a day ago

    Note that the author is not referring to the accept-reject method here

egorfine a day ago

Please forgive me my naivete, but won't generating two random polar coordinates do? I'm bad at math, so I might as well be very very wrong here, but I'd like to know.

Edit: see @srean's excellent explanation why that won't do.

  • srean a day ago

    If you want uniformly random on the spherical surface then uniformly at random in polar coordinates will not cut it.

    To appreciate why, consider strips along two constant latitudes. One along the Equator and the other very close to the pole. The uniformly random polar coordinates method will assign roughly the same number points to both. However the equatorial strip is spread over a large area but the polar strip over a tiny area. So the points will not be uniformly distributed over the surface.

    What one needs to keep track of is the ratio between the infinitesimal volume in polar coordinates dphi * dtheta to the infinitesimal of the surface area. In other words the amount of dilation or contraction. Then one has apply the reciprocal to even it out.

    This tracking is done by the determinant of the Jacobian.

    • egorfine a day ago

      This is now crystal clear and obvious to me, thank you very much for the great explanation!

      • srean a day ago

        Happy to help.

  • danwills a day ago

    I think it can be done that way yeah but in order to yield a uniform-density of points on the surface of the sphere there's some pre-correction (maybe a sqrt or something? I can't remember) that's needed before feeding the 'uv' values to the trig functions to make 3D positions. Otherwise points will 'bunch up' and be more dense at the poles I think.

    • srean a day ago

      Indeed.

      One way to fix the problem is to sample uniformly not on the latitude x longitude rectangle but the sin (latitude) x longitude rectangle.

      The reason this works is because the area of a infinitesimal lat long patch on the sphere is dlong x lat x cosine (lat). Now, if we sample on the long x sin(lat) rectangle, an infinitesimal rectangle also has area dlong x dlat x d/dlat sin(lat) = dlong x dlat cos (lat).

      Unfortunately, these simple fixes do not generalize to arbitrary dimensions. For that those that exploit rotational symmetry of L2 norm works best.

    • egorfine a day ago

      Generating two random 1..360 numbers and converting them to xyz would bunch up at the poles?

      • danwills a day ago

        Yeah @srean gives the example of the different areas of strips at different lattitude, that's a good one - and I think if you imagine wrapping the unit square (2 values randomly between 0 and 1) to a sphere in a lat-long way, the whole top and bottom edges of the square get contracted to single points at the top and bottom latitude locations (respectively) on the sphere.. so if the point density was uniform going into that then it surely won't be afterwards ;)

    • egorfine a day ago

      See @srean's explanation above.

pavel_lishin 2 days ago

> First, it’s intuitively plausible that it works.

Maybe; my first instinct is that there'll be some bias somewhere.

Maybe I'll have some time tonight to play with this in p5js.

  • sparky_z a day ago

    That was my first instinct as well, but I thought through it a little more and now it seems intuitively correct to me.

    -First of all, it's intuitive to me that the "candidate" points generated in the cube are randomly distributed without bias throughout the volume of the cube. That's almost by definition.

    -Once you discard all of the points outside the sphere, you're left with points that are randomly distributed throughout the volume of the sphere. I think that would be true for any shape that you cut out of the cube. So this "discard" method can be used to create randomly distributed points in any 3d volume of arbitrary shape (other than maybe one of those weird pathological topologies.)

    -Once the evenly distributed points are projected to the surface of the sphere, you're essentially collapsing each radial line of points down to a single point on the sphere. And since each radial line has complete rotational symmetry with every other radial line, each point on the surface of the sphere is equally likely to be chosen via this process.

    That's not a rigorous proof by any means, but I've satisfied myself that it's true and would be surprised if it turned out not to be.

    • pavel_lishin 19 hours ago

      To me, it seems like there would be less likelihood of points being generated near the surface of the sphere, and that should have some sort of impact.

      • sparky_z 19 hours ago

        OK, look at it this way. Imagine that, after you generate the points randomly in the cube, and discard those outside the sphere, you then convert the remaining points into 3D polar coordinates (AKA spherical coordinates [0]). This doesn't change the distribution at all, just the numerical representation. So each point is described by three numbers, r, theta, and phi.

        You're correctly pointing out that the values of r won't be uniformly distributed. There will be many more points where the value of r is close to 1 then there will be where the value of r is close to 0. This is a natural consequence of the fact that the points are uniformly distributed throughout the volume, but there's more volume near the surface than there is near the center. That's all true.

        But now look at the final step. By projecting every point to the surface of the sphere, you've just overwritten every single point's r-coordinate with r=1. Any bias in the distribution of r has been discarded. This step is essentially saying "ignore r, all we care about are the values of theta and phi."

        [0]https://en.wikipedia.org/wiki/Spherical_coordinate_system

  • pavel_lishin 2 days ago

    I had some time!

    It looks reasonably random to my eye: https://editor.p5js.org/fancybone/sketches/DUFhlJvOZ

    • guccihat a day ago

      Cool demo. A minor nitpick is that the code (and the article) forgets to handle the special case of a point inside the cube that happens to be exactly (0,0,0). This will result in a divide by zero when the vector is normalized.

      • pavel_lishin 19 hours ago

        That nitpick is both minor, and absolutely correct!

      • NoahZuniga a day ago

        The chance of this happening is less than 1 in 2^128. This will never happen.

        • pavel_lishin 19 hours ago

          Unless you're demoing it to someone very important, in which case it'll happen twice in a row.