473,698 Members | 1,846 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 5896
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 "geometrica l 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
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
4209
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
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
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...
0
7708
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
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
4358
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...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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.