Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm caring monte-carlo simulations and I am checking my code for some faults. I have just realized (the hard way) that to generate a unit vector pointing in a random direction I cannot simply pick 3 (or any other number) random number and normalize them. I didn't understand why I can't do this apart from the claim that the generated vector doesn't have even probability to point at any direction. I can't find the math behind there and I suspect that this is mainly due to lack of proper term to search. What should I search for? Where can I find explanation? Preferably without assumption of prior knowledge.

share|improve this question

migrated from Dec 19 '12 at 19:47

This question came from our site for active researchers, academics and students of physics.

@tpg2114 I wasn't sure where should I ask this. I decided to ask here because I suspect that physicist will encounter this kind of problems. – Yotam Dec 19 '12 at 18:17
Yes, but statisticians will be better qualified to answer this. – Emilio Pisanty Dec 19 '12 at 18:30
Well, I would first say that you're doing too much calculation. Generate one angle between 0 and 360 and another angle between 0 and 180 and you're done. And then it shoudl be extremely obvious that all of your probabilities are equal. To see the other thing, you're going to have to calculate things like $\phi = \tan^{-1}\frac{y}{x}$ and propogate your normal distribution through the function. You'll see that the answer is not normal. – Jerry Schirmer Dec 19 '12 at 18:36
@Jerry: Generating one angle between 0 and 360 and another between 0 and 180 DOES NOT WORK! A point near the pole is much more likely than one near the equator. – Peter Shor Dec 19 '12 at 19:25
The generation of random points on the 2-sphere is discussed in detail and illustrated at A variant of @jerry schirmer's approach actually does work: you can generate points uniformly using any equal-area projection, such as the equal area rectangular (in which the longitude is uniformly distributed between $0$ and $2\pi$ and the cosine of the latitude is uniformly distributed between $-1$ and $1$). – whuber Dec 20 '12 at 2:28

A good way to get a random direction on a 2-sphere might be to choose $z$ uniform in $[-1,1]$, and $\theta$ uniform in $[0,2\pi]$. Then take the point $$ (\sqrt{1-z^2} \cos \theta, \sqrt{1-z^2} \sin \theta, z).$$

I won't do the math to show why these give points uniformly on a sphere. It's not hard.

For large dimensions, the best way is probably to choose $n$ samples from a Gaussian distribution, $x_1 \ldots, x_n$, and normalize the resulting vector $(x_1, x_2, \ldots,x_n)$. You can see why this works by looking at the probability density function. The function for a Gaussian is $$\frac{1}{\sqrt{2 \pi}} e^{-\frac{1}{2}x^2},$$ so when you multiply $n$ of them you get $$\frac{1}{(2 \pi)^{n/2}} e^{-\frac{1}{2}\sum_i x_i^2},$$ which is clearly spherically symmetric.

share|improve this answer
Cool result indeed! – Jaime Dec 19 '12 at 21:12

If you choose 3 independent random numbers between -1 and 1, you are basically choosing a point inside a cube. From that image, you can easily see that, after normalization, the likelihood of your vector pointing along the cube main diagonal is $\sqrt{3}$ larger than that of it pointing along the $x$ axis, simply because there are $\sqrt{3}$ more points inside the cube along the main diagonal than along the direction of any coordinate axis.

As Jerry Schirmer points out, you can take two angles, and build your vector from there, basically using spherical coordinates. The idea is also developed here. Alternatively, you could still generate your three uniform random numbers, and before normalizing them, get rid of them if $x^2 + y^2 + z^2 > 1$, thus effectively limiting your 3-D point to within a sphere.

As for general reading on the subject, you want to look for sampling an arbitrary distribution, although most of what you will find will be for one-dimensional distributions, probably the most general method being inverse transform sampling.

share|improve this answer
Or, for much larger dimensions, you could choose $n$ points from a Gaussian distribution with mean 0 and variance 1, and normalize them. This gives a random direction. – Peter Shor Dec 19 '12 at 19:24
Did not know that, but it is an über-cool result! Do you have any reference, or care to explain it in answer? I promise to upvote... – Jaime Dec 19 '12 at 19:28
For one dimension, inverse transform sampling is indeed a good thing to use. The problem with discarding points outside a sphere is that in high dimensions, you end up discarding nearly 100% of your points. – Peter Shor Dec 19 '12 at 19:41
Schirmer's approach is incorrect, unfortunately. – whuber Dec 20 '12 at 2:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.