I've been thinking about this question for a long time. I've looked in my intro C and Java books, but I still haven't found an answer.
Let's say you want to move a particle in 3 dimensions by some unit distance. In other words, we're just dealing with a sphere of movement. Immediately I thought of generating a random number between 0 and 1 and multiplying that by some values
1) For theta, random * pi
2) For phi, random * 2pi
Where theta is from pole to pole and phi is from 0 to 2pi. However, this technique does not work. If you repeat it several times, you artificially obtain a lot of movement toward the poles. The distribution is uneven somehow. Instead, this is a solution.
1) For costheta, 2*random - 1
2) For sintheta, sintheta = sqrt(1-(costheta)^2)
3) For phi, random * 2pi
This generates an even 3d distribution, resulting in random 3d motion whereas the first idea did not. Can anyone figure out why this is?
Let's say you want to move a particle in 3 dimensions by some unit distance. In other words, we're just dealing with a sphere of movement. Immediately I thought of generating a random number between 0 and 1 and multiplying that by some values
1) For theta, random * pi
2) For phi, random * 2pi
Where theta is from pole to pole and phi is from 0 to 2pi. However, this technique does not work. If you repeat it several times, you artificially obtain a lot of movement toward the poles. The distribution is uneven somehow. Instead, this is a solution.
1) For costheta, 2*random - 1
2) For sintheta, sintheta = sqrt(1-(costheta)^2)
3) For phi, random * 2pi
This generates an even 3d distribution, resulting in random 3d motion whereas the first idea did not. Can anyone figure out why this is?