There is a familiar scheme for choosing a random direction in n-dimensions that works well for small n. Select successive xi from U{−1, 1} until Σxi2 > 1 or until we have n samples. Discard the samples and start over when you exceed 1. When n samples have been achieved the point whose coordinates are xi is in a random direction.

This is a very useful algorithm but its efficiency degrades rapidly when n is greater than about 6. It becomes too hard to not exceed 1.

Instead we choose n random normal deviates xi and the point that they define in n-space is already is a random direction from the origin.

To generate two independent random normal deviates choose random points <x, y> uniformly from the square [−1, 1] × [−1, 1] until r2 = x2 + y2 < 1. Let q = √(−2 ln(r2))/r2. xq and yq are independent random normal deviates.

The C routine rnd in this program produces random normal deviates. This Scheme code which employes some random number generator such as rand31 here also does this algorithm. The algorithm came from Knuth.

See this for choosing a point within the nD ball.