473,563 Members | 2,722 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 5873
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.a lgorithms or comp.programmin g.

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

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

Similar topics

3
2675
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
4193
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
7364
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
2995
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
4215
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
1654
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
3176
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
2782
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
7880
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
7659
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
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
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...
1
7634
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...
0
7945
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
3634
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...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
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
0
916
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.