Random numbers within a sphere?

R

Ron Natalie

Stavros might need evenly distributed numbers. A polar coordinate scheme
would bunch numbers up around the center, at least.
How so? If the [0, sphere-radius] random number generator is uniform, so would
the density with respect to the center.
The fix is to bound the sphere with a cube, tangent on all faces, and find
random points in the cube by finding random xyz points, each >= -r and < r,
where r is the radius of the sphere. Then filter out all points laying
outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.

That wouldn't be any better and a lot slower as a lot of samples would have
to be generated, tested, and discarded.
 
S

Stavros Christoforou

Hello everyone,

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

Regards
Stavros
 
F

Fabio Rossi

Stavros said:
Hello everyone,

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

You could represent any point of the sphere in spherical coordinates: a
radius and two angles. A coordinate is a uniform variable (using rand()) in
its [min,max] interval.
 
V

Victor Bazarov

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

Select a random radius (between 0 and the sphere radius), a randon azimuth
(0 through 2*Pi) and a random elevation (-Pi/2 through Pi/2) and then
convert the three values from spherical coordinates into Cartesian (if
that's your desired result). And please don't post off-topic questions
here. Your question has nothing to do with C++ language and ought to be
posted in comp.graphics.algorithms or comp.programming.

V
 
P

Phlip

Fabio said:
Stavros said:
Hello everyone,

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

You could represent any point of the sphere in spherical coordinates: a
radius and two angles. A coordinate is a uniform variable (using rand()) in
its [min,max] interval.

Stavros might need evenly distributed numbers. A polar coordinate scheme
would bunch numbers up around the center, at least.

The fix is to bound the sphere with a cube, tangent on all faces, and find
random points in the cube by finding random xyz points, each >= -r and < r,
where r is the radius of the sphere. Then filter out all points laying
outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.
 
V

Victor Bazarov

Ron said:
Stavros might need evenly distributed numbers. A polar coordinate scheme
would bunch numbers up around the center, at least.

How so? If the [0, sphere-radius] random number generator is uniform,
so would
the density with respect to the center.
The fix is to bound the sphere with a cube, tangent on all faces, and
find
random points in the cube by finding random xyz points, each >= -r and
< r,
where r is the radius of the sphere. Then filter out all points laying
outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.


That wouldn't be any better and a lot slower as a lot of samples would have
to be generated, tested, and discarded.

"A lot of samples"? The sphere is about half the volume of its
circumscribing cube. So, less than half of all samples are going to
be discarded. Doesn't seem like "a lot" to me. Of course, everybody
has their own definition of "a lot". Is there a better way to produce
points evenly distributed in the volume of a sphere?

V
 
S

Shezan Baig

Ron said:
Stavros might need evenly distributed numbers. A polar coordinate scheme
would bunch numbers up around the center, at least.
How so? If the [0, sphere-radius] random number generator is uniform, so would
the density with respect to the center.


No, the probability of being close to the center is higher than the
probability of being around the edges.
 
S

Stavros Christoforou

No, the probability of being close to the center is higher than the
probability of being around the edges.

May I ask why?
Stavros might need evenly distributed numbers. A polar coordinate
scheme would bunch numbers up around the center, at least.
How so? If the [0, sphere-radius] random number generator is uniform, so would the density with respect to the center.


The density would be selected from a pdf, eg

s = - (log ((double) (rand()+ 1.0)/RAND_MAX))/sigmat

(just a random function)

Therefore my problem focuses more on how to select the points within the
sphere, and my idea so far was similar to Philip's. However, I am sure
something faster and more "code-correct" exists.

Also, sorry if I posted this on the wrong group, but as I am creating
the program on C++ I thought that this would be the appropriate place to
ask questions.


Stavros
 
S

Shezan Baig

Stavros said:
May I ask why?


It might be a little hard to explain without graphical aid :)

But basically, to be around the center, you need to have a small
radius. The angles don't really matter here - as long as the radius is
small, then the point will be near the center.

To make the point closer to a particular edge, you need a bigger radius
*and* the correct angle (or direction, or whatever). So the
probability of this happening is less.

Hope this helps,
-shez-
 
K

Karl Heinz Buchegger

Ron said:

The problem is that the mean distance between 2 neighboring points is
smaller near the center then it is at the circumference. So the number
of points near the center is packed tighter -> the point density (nr of points
per partial volume) is higher. Depending on the application this might
or might not be acceptable.
If the [0, sphere-radius] random number generator is uniform, so would
the density with respect to the center.

The fix is to bound the sphere with a cube, tangent on all faces, and find
random points in the cube by finding random xyz points, each >= -r and < r,
where r is the radius of the sphere. Then filter out all points laying
outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.

That wouldn't be any better and a lot slower as a lot of samples would have
to be generated, tested, and discarded.

.... Yes, but it generates a uniform volume sampling of the sphere. Something
the radius/angle method doesn't do.
 
K

Karl Heinz Buchegger

Shezan said:
It might be a little hard to explain without graphical aid :)

But basically, to be around the center, you need to have a small
radius. The angles don't really matter here - as long as the radius is
small, then the point will be near the center.

To make the point closer to a particular edge, you need a bigger radius
*and* the correct angle (or direction, or whatever). So the
probability of this happening is less.

What 'edges' are you talking about?
 
K

Karl Heinz Buchegger

Shezan said:
Sorry, I meant a point near the circumference.

In this case your argumentation doesn't apply. If a sampled
radius is 80% of the speheres radius, then it will be 20% from
the surface, no matter which direction.
 
J

Joseph Seigh

Fabio said:
Stavros said:
Hello everyone,

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

You could represent any point of the sphere in spherical coordinates: a
radius and two angles. A coordinate is a uniform variable (using rand()) in
its [min,max] interval.

Stavros might need evenly distributed numbers. A polar coordinate scheme
would bunch numbers up around the center, at least.

The fix is to bound the sphere with a cube, tangent on all faces, and find
random points in the cube by finding random xyz points, each >= -r and < r,
where r is the radius of the sphere. Then filter out all points laying
outside the sphere, by comparing x^2 + y^2 + z^2 to r^2.
That's not likely to be an even distribution since you're discarding samples.
The OP need to define what the set of "points" is in the fist place, which
would depend on the coordinate system chosen. The OP would also need to
define the function f(x, y), where x and y are "points", against which the
random distribution is being applied. It could be metric distance between
the points or something else entirely. The OP really needs to repost their
question in comp.programming where they deal with these kind of questions
rather than there where it's a little off topic.
 
S

Stavros Christoforou

Joseph Seigh wrote:
The OP really needs to repost their
question in comp.programming where they deal with these kind of questions
rather than there where it's a little off topic.

I see. Thank you all for your replies, I'll give it a shot there!

Stavros
 
P

Pete Becker

Joseph said:
That's not likely to be an even distribution since you're discarding
samples.

It's a very common technique: generate values that are evenly
distributed and discard ones that aren't in the desired range. The
remaining values are as evenly distributed in the target range as the
original ones were in their range.
 
P

phlip2005

Pete said:
Joseph Seigh wrote:

It's a very common technique: generate values that are evenly
distributed and discard ones that aren't in the desired range. The
remaining values are as evenly distributed in the target range as the
original ones were in their range.

For example, if Pete and me post to a newsgroup for a decade, and your
filter in all the posts where he backs up one of my wild opinions, the
posts will have the same distribution. Except over a much smaller set.
;-)
 
M

Michael

Off the top of my head, I think you'd need to look at the partial deriative
of the volume with respect to radius, ie, at a particular distance see what
a delta R did to delta V...... but I've got to go to lectures , ( and a bit
OT!)

Nice probelm tho!!

Mike

Stavros Christoforou said:
No, the probability of being close to the center is higher than the
probability of being around the edges.

May I ask why?
Stavros might need evenly distributed numbers. A polar coordinate
scheme would bunch numbers up around the center, at least.
How so? If the [0, sphere-radius] random number generator is uniform, so would the density with respect to the center.


The density would be selected from a pdf, eg

s = - (log ((double) (rand()+ 1.0)/RAND_MAX))/sigmat

(just a random function)

Therefore my problem focuses more on how to select the points within the
sphere, and my idea so far was similar to Philip's. However, I am sure
something faster and more "code-correct" exists.

Also, sorry if I posted this on the wrong group, but as I am creating
the program on C++ I thought that this would be the appropriate place to
ask questions.


Stavros
 
P

phlip2005

How so? If the [0, sphere-radius] random number generator is uniform, so
would the density with respect to the center.

Instead of random numbers, imagine evenly spaced numbers on a line. Put
the line from the center to the rim of a circle. Add a point to the
circle's plane for each number on the line. Now rotate the line so it
reaches from the center to another point on the rim. Stamp out more
points onto the circle.

Keep going until you have covered the rim in an even number of steps.
Notice you drew N dotted circles, one for each number on the original
line. Now notice how the dots near the center are closer together.

Arbitrarily plugging evenly spaced pseudorandom numbers into polar
coordinates, with no other adjustments, will bunch the numbers up
around the center.
That wouldn't be any better and a lot slower as a lot of samples would have
to be generated, tested, and discarded.

Only slower by the volume of a cube minus the volume of a tangent
sphere. Most points will be inside the sphere.

The performance would still only be dominated by the speed of
generating pseudo-random numbers, and the speed of comparing their
distances. (And note I left the Square Root calculation out of the
distance formula.)
 
A

Andrey Tarasevich

Ron said:
Stavros might need evenly distributed numbers. A polar coordinate scheme
would bunch numbers up around the center, at least.
How so? If the [0, sphere-radius] random number generator is uniform, so would
the density with respect to the center.

As always with geometrical probability, what constitutes "even
distribution" is not as obvious as it might seem at the first sight. It
depends on the choice of metric. That's exactly the essence of the
misunderstanding that takes place in this discussion. For an
illustration look up "bertrand paradox" (or "bertrand's paradox") on
Google, it is directly relevant to the ongoing discussion (see, for
example, here http://www.cut-the-knot.org/bertrand.shtml).
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top