A
Andrey Tarasevich
Stavros said:I was wondering if someone could help me with an issue I have in C++. I
want to select random points within the volume of a sphere. I know how
to get random numbers using srand() and rand(), but have no idea how to
do that within a more complicated geometry. Any help would be greatly
appreciated..
...
This is an off-topic question.
But anyway, in order to devise a method of generating random numbers
within certain "geometrical object" (a 3D sphere, a 2D circle or
something else) you have to decide what kind of distribution you would
like to obtain. Depending on the application, this might affect your
results significantly. What is the primary purpose of that random point
generator?
Imagine, for example, that your sphere has radius R. And also that
there's another sphere inside that first one with the same center and
radius R/2. Let's say we want to use Monte Carlo method in order to
determine how many times the [Euclidean] volume of the inner sphere is
smaller than the volume of the larger sphere. It is simple: we just
generate a large number of random points (N points) inside the outer
sphere and count the ones that get into the inner sphere (M points). In
this case the volume ratio would be M/N.
Now, if you generate the points as '(x = rand(), y = rand(), z =
rand())' and then filter away ones outside the outer sphere, you'll
finally obtain the value of M/N close to 1/8, which is the correct answer.
However, if you use the "polar" method described by other posters (two
random angles and a random radius), you'll arrive at the value of M/N
close to 1/2, which appears to be incorrect. In fact, both answers are
correct in their own way. The problem with the second approach is that
the metric associated with this method of generating random points is
non-Euclidean, and therefore is not what is needed for this task. It is
not "incorrect", it is simply incorrectly applied.
Surprising effects induced by different choices of metric in geometrical
probability play their roles in rather well-known "Bertrand's Paradox"
(see http://www.cut-the-knot.org/bertrand.shtml, for example).
Once again, the choice of method in general case depends on the
requirements of the application. Since you didn't provide any details,
it is hard to come up with a concrete advice.