473,516 Members | 3,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random numbers within a sphere?

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
Jul 23 '05 #1
24 5866
Stavros Christoforou wrote:
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.
Jul 23 '05 #2
Stavros Christoforou wrote:
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
Jul 23 '05 #3
Fabio Rossi wrote:
Stavros Christoforou wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 23 '05 #4

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.

Jul 23 '05 #5
Ron Natalie wrote:
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
Jul 23 '05 #6

Ron Natalie wrote:

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.

Jul 23 '05 #7
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
Jul 23 '05 #8

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


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-

Jul 23 '05 #9
Ron Natalie wrote:

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


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #10
Shezan Baig wrote:

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


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.


What 'edges' are you talking about?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #11

Karl Heinz Buchegger wrote:
Shezan Baig wrote:

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

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.


What 'edges' are you talking about?

--
Karl Heinz Buchegger
kb******@gascad.at

Sorry, I meant a point near the circumference.

Jul 23 '05 #12
Shezan Baig wrote:

Karl Heinz Buchegger wrote:
Shezan Baig wrote:

Stavros Christoforou wrote:
> > No, the probability of being close to the center is higher than the > > probability of being around the edges.
> >
>
> 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.


What 'edges' are you talking about?


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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #13
On Tue, 08 Feb 2005 14:18:56 GMT, Phlip <ph*******@yahoo.com> wrote:
Fabio Rossi wrote:
Stavros Christoforou wrote:
> 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.

--
Joe Seigh

http://atomic-ptr-plus.sourceforge.net/
Jul 23 '05 #14
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
Jul 23 '05 #15
Joseph Seigh wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #16
Pete Becker wrote:
Joseph Seigh wrote:
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.


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

--
Phlip

Jul 23 '05 #17
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" <S.**********************@iri.tudelft.nl> wrote in
message news:1f***************************@news1.tudelft.n l...
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

Jul 23 '05 #18
Ron Natalie wrote:
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.
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.
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.


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

--
Phlip

Jul 23 '05 #19
Ron Natalie wrote:
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).

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #20
Stavros Christoforou wrote:
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.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #21
Andrey Tarasevich wrote:
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 news:sci.math ... ;-)

--
Phlip

Jul 23 '05 #22
Phlip wrote:
Andrey Tarasevich wrote:

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 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
Jul 23 '05 #23
On Wed, 8 Feb 2005, Phlip 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.


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/

Jul 23 '05 #24
Peter Jansson wrote:
Phlip 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.
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!

--
Phlip

Jul 23 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2674
by: Heath | last post by:
Using MSIE 5+ Heath writes: My problem deals with working with window objects between pages as follows: My Introduction page contains a link to my Action page. The onClick of that link creates a series of random numbers that are appended to the end of the url of the Action page. After initiating the link the URL of the Action page looks...
16
4188
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is...
3
7357
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The...
21
2993
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
13
4214
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number between 0 and 100? (call rand() only 10 times...) Thanks, qq
4
1646
by: Maziar Aflatoun | last post by:
Hi everyone, I have the following code in my class method TheSeed = (int)DateTime.Now.Ticks; Random rndNum = new Random(TheSeed); RandNum = rndNum.Next(0, TotalRows); Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows); My page gets called everytime and these are the values that I'm getting for
13
3163
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister, with it's massive period of 2^19937-1. Will you only ever have access to a tiny portion of this ring of numbers, if you only use a 32-bit seed? Will...
13
2770
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: ...
26
7871
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand()); int r2 = rand(); bool f = r1 == r2;
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7574
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7547
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5106
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.