# Get random $x, y, z$ point inside a sphere?

What is the easiest way to get a random Point3D inside the sphere where is red?

-
Is the red region only on the surface of the sphere, or does it extend inwards? If so, how? (like an ice-cream cone?) – Nick Alger Dec 14 '11 at 7:50
inwards, not on the face, inside the sphere – lacas82 Dec 15 '11 at 20:30
So, like a spherical cap? Or like an ice-cream cone? – Nick Alger Dec 16 '11 at 1:00

Use rejection sampling. Generate a candidate point in the sphere. If it so happens that the point is in the red region, accept it. If not then reject it and generate another candidate. After $n$ candidates are generated, roughly $np$ are accepted, where $p$ is the measure of the proportion of the sphere which is red.

-

Say this is the sphere centered on the origin with radius 1, and your red area is that part of the sphere with $z > z_0$.

It turns out that if you generate a point $(u,v,z)$ uniformly at random on the cylinder with $u^2+v^2 = 1$ and $-1 \le z \le 1$ (the cylinder circumscribing the sphere) and then project it horizontally onto the sphere, taking

$$\left( \sqrt{1-z^2} u, \sqrt{1-z^2} v, z \right)$$

then you have generated a point uniformly at random on the sphere. And of course you can generate $(u,v)$ as $(\cos \theta, \sin \theta)$ where $\theta$ is chosen uniformly at random in $(0, 2\pi)$.

If you restrict $z$ to be in $[z_0, 1]$, then this reduces to the case that you're talking about.

Therefore:

• take $z$ uniformly at random in $[z_0, 1]$

• take $\theta$ uniformly at random in $[0, 2\pi]$

• return the point $\left( \sqrt{1-z^2} \sin \theta, \sqrt{1-z^2} \cos \theta, z \right)$.

-
That's a very nice way to get a point on the sphere, but he did ask about a point inside the sphere. I have no idea which points inside the sphere are "red". – Robert Israel Dec 13 '11 at 18:19
It appears that I should read questions more carefully before answering them. (My method probably can be adapted there, at least in the case where the red points are all those on one side of a plane, but I don't feel like doing it right now.) – Michael Lugo Dec 13 '11 at 19:41

This part of the shepre is isomorphic to $[a,b]\times [c,d]$ by the transformation $(sin(\theta)cos(\phi), sin(\theta)sin(\phi), cos(\theta)$ for $\phi \in [a,b], \theta \in [c,d]$.

So find the right $a,b,c,d$ by finding the range of the angles, random a number from $[a,b]\times [c,d]$ and use this transformation to get the coordinate on the shpere.

-