• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question regarding generation of random angles

Eeezee

Diamond Member
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.
 
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. 😛
 
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. 🙁
 
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.
 
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
 
Back
Top