Random numbers within a sphere?

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.
 
P

Phlip

Andrey said:
This is an off-topic question.

I want to know how to select a random number (or pseudo-random number)
that falls between zero and infinity, with a linear probability
distribution.

But I'm too CS to ask ... ;-)
 
V

Victor Bazarov

Phlip said:
Andrey Tarasevich wrote:




I want to know how to select a random number (or pseudo-random number)
that falls between zero and infinity, with a linear probability
distribution.

But I'm too CS to ask news:sci.math ... ;-)

You could try normalizing the value, then taking a square root, then
scaling to the desired interval. Don't quote me on that, though.

V
 
P

Peter Jansson

I want to know how to select a random number (or pseudo-random number)
that falls between zero and infinity, with a linear probability
distribution.

Me too! And if you figure it out, plase let me know how you intend to save
your selected number for future reference if, for example, it contains
10^(10^100) digits.

Regards,
Peter Jansson
http://www.jansson.net/
 
P

Phlip

Peter said:
Me too! And if you figure it out, plase let me know how you intend to save
your selected number for future reference if, for example, it contains
10^(10^100) digits.

Why, I would store it on www.Google.com , of course!
 
Joined
Oct 23, 2007
Messages
1
Reaction score
0
How about this?

/* Polar coordinates. Transforms of random numbers. */
double radius, azimuth, zenith;
radius= pow( (double)rand() /RAND_MAX, 1.0/3.0);
azimuth= 2.0 *M_PI *rand()/RAND_MAX;
zenith= acos( 2.0 *rand()/RAND_MAX -1.0);

/* Cartesian coordinates */
double xy, x, y, z;
xy= radius *sin(zenith);
x= xy *cos(azimuth);
y= xy *sin(azimuth);
z= radius *cos(zenith);


The code generates random points evenly distributed within the unit sphere.

Richard Ayling
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top