I need to create a method to generate a unit vector in three dimensions that points in a random direction using a random number generator. The distribution of direction MUST be isotropic.
Here is how I am trying to generate a random unit vector:
v = randn(1,3);
v = v./sqrt(v*v');
But I don't know how to complete the isotropic part. Any ideas?
Asked
Active
Viewed 1.9k times
21

Aina
- 653
- 2
- 9
- 22
-
possible duplicate of [Uniform random (Monte-Carlo) distribution on unit sphere](http://stackoverflow.com/questions/1841014/uniform-random-monte-carlo-distribution-on-unit-sphere) – finnw Mar 22 '12 at 13:09
-
There are also some good answers [here](http://stackoverflow.com/questions/8839086/how-to-randomize-points-on-a-sphere-surface-evenly) and [here](http://stackoverflow.com/questions/6389984/how-to-randomly-select-a-point-on-the-surface-of-the-earth) – finnw Mar 22 '12 at 13:14
-
@finnw: The question is a duplicate, but not the answers. Maybe we could merge the questions? – Jonas Mar 24 '12 at 13:29
1 Answers
22
You're doing it right. A random normal distribution of coordinates gives you a uniform distribution of directions.
To generate 10000 uniform points on the unit sphere, you run
v = randn(10000,3);
v = bsxfun(@rdivide,v,sqrt(sum(v.^2,2)));
plot3(v(:,1),v(:,2),v(:,3),'.')
axis equal

Jonas
- 74,690
- 10
- 137
- 177
-
-
@Aina: The 2D normal distribution is rotationally symmetric. Thus, the 3D normal distribution has spherical symmetry. – Jonas Mar 17 '12 at 23:51
-
-
3@Andrey: Why the normal distribution is rotationally symmetric, you mean? The 3D normal distribution is proportional to `exp(- (x^2+y^2+z^2))`. The symmetry becomes obvious if you transform to spherical coordinates, where that expression becomes `exp(-(r^2))`. In other words, the density is only a function of the radius, not the angle, which means the points are uniformly distributed among all angles. – Jonas Mar 24 '12 at 13:25
-
@Jonas As far as I can see, this is an excellent method. Would you be able to produce a version for R? Thanks. – JASC Jan 14 '19 at 03:29
-
1@JASC: Sadly no, as I'm just barely literate in R. However, the algorithm is really (1) draw a number of points from a 3D normal distribution (i.e. a list of coordinates where x, y, and z are drawn from independent normal distributions with mean 0 and standard deviation 1), and then divide each coordinate triplet by its norm so that x^2+y^2+z^2 is 1 for each of them. – Jonas Jan 14 '19 at 17:15
-
-
@user1438310: yes, it does, as the relationships hold for cubes/spheres of any dimension. – Jonas Dec 20 '20 at 13:27