Question regarding generation of random angles

Eeezee

Diamond Member
Jul 23, 2005
9,923
0
0
Let's say I want a particle to move randomly in 3d space. I have to do the following

1) Generate random theta direction
2) Generate random phi direction

Well, this should be as simple as generating a random number between 0 and 1 and multiplying that by 2*pi and then repeating this for pi. That doesn't work so well. If we do this, we don't get an even 3d distribution of movement. Instead, you get mostly movement towards the poles.

Here is a solution

1) Generate random phi direction (2*pi*random)
2) Generate random cos(theta) (2*random - 1)
3) Generate sin(theta) = sqrt(1-(costheta)^2)

Does anyone know why, from first principles, it has to be done this way.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
In effect, it's because the sin/cos functions are nonlinear. If X is a uniform distribution from 0->pi, sin(X) and cos(X) have very different statistical properties from X itself. The solution you gave is one way of getting around the nonlinearity.

Proving it is way more complex than I want to deal with right now.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
The way I would approach this problem (if I were going to generate angles rather than simply coordinates) is to relate the random number seed to an angle using a binning system. The results you get will also depend on the random number generator.

I've actually done this before for a 3-d random walk simulation. I'll upload the spreadsheet and you can see how I did it. Essentially I used three random seeds to decide where the particle would move in each of the three dimensions. Since these bases are orthogonal and linear, there is no bias in the problem and infinitely many repetitions should give a normal distribution based on the mobility of the particle. It's trivial to convert these results into spherical coordinates, so I'm sure you can make the jump in that direction.

edit: The university, in its infinite wisdom, has expired my password for my campus website login. Can't upload.
 

CSMR

Golden Member
Apr 24, 2004
1,376
2
81
Originally posted by: Eeezee
Let's say I want a particle to move randomly in 3d space. I have to do the following

1) Generate random theta direction
2) Generate random phi direction

Well, this should be as simple as generating a random number between 0 and 1 and multiplying that by 2*pi and then repeating this for pi. That doesn't work so well. If we do this, we don't get an even 3d distribution of movement. Instead, you get mostly movement towards the poles.
Don't say random when you mean uniformly distributed.
 

CSMR

Golden Member
Apr 24, 2004
1,376
2
81
First you should say what you want. I suppose it is the polar coordinates of a random point uniformly distributed in the unit sphere.

the map from spherical coordinates to cartesian is:
C(r,theta,phi)=(r cos theta cos phi,r sin theta cos phi,r sin phi)
(column vectors)
The jacobean |determinant(C'(r,theta,phi))|=r^2 sin phi
according to mathworld.
This means that the density function of the spherical coordinates is (r^2 sin phi) times the density function of the cartesion coordinates, so is proportional to (r^2 sin phi).
It is a product of functions of r, theta and phi, so these are independent and theta is uniformly distributed (constant denity) and phi is distributed with density function proportional to sin(phi), so density function sin(phi)/2.
Then phi has distribution function F(x)=(cos(x)+1)/2. F^-1(U)=cos^-1(2U-1) has this distibution function where U is uniform on [0,1).

I am using the definitions from mathworld which I presume are the standard ones. phi and thetat are reversed.
http://mathworld.wolfram.com/SphericalCoordinates.html
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |