473,698 Members | 1,997 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
24 5897
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*******@yaho o.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.programmin g 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.programmin g 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.tude lft.nl...
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
misunderstandin g 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

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

Similar topics

3
2677
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 like this:
16
4210
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 sufficiently random. Can anybody help me out in getting what I want from VC++?
3
7378
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 code generates a seed based on time(0). I have found that I need to increment
21
3010
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
4226
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
1657
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
3189
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 this set of sequences be confined to the beginning of the period, ie, your sequence will have to...
13
2803
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: unsigned long Random( unsigned long num )
26
7901
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
9016
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5858
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3037
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
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.