# How to build a random vector perpendicular to another vector in R3

1. Aug 19, 2014

### Chandrasekhar

(SOLVED) How to build a 3D random vector perpendic. to another vector

Hi everybody,

do you have an efficient method for build up a vector with random components which is perpendicular to another (unitary) 3D vector ?

Context: I have to randomly select polarization vector (P) for unpolarized photons propagating on a certain direction (K) and the only condition is P being orthogonal to K.

input: K = (Kx, Ky, Kz) (it can be normalized if needed)
output: P = (Px, Py, Pz), P must be perpendicular to K and also unitary (modulus = 1)

I guess the Gram-Schmidt process must be used BUT the important thing is that P has to be randomly chosen on the plane perpendicular to K among the infinite possibilities with equal probability.

My photons propagates toward a preferential direction so if I chose everytime Px = Kx for starting the process, I'm giving a preferential orientation to P which is not the case.

One more thing is that I have to do this in a code so if you want to put the answer in the form of an algorithm it would be really appreciated!
Otherwise consider the use of random variables at some point in your explanation.

Thank you very much in advance! :)

------------------------------------------
one possible solution (???)

I choose a completely random vector, R, on a sphere centered at the origin of the reference frame of K.
Then I make the cross product between R and K and I should obtain P perpendicular to K (and R) but randomly chosen..

P = K x R

The question now is: does a random selection on a sphere produce a random vector on the plane perpendicular to K via cross product ?

Last edited: Aug 19, 2014
2. Aug 19, 2014

### Erland

This will work, as far as I can see... (provided that you can you can choose a random vector on sphere, from a uniform distribution w.r.t. surface area, which does not seem to be trivial to me).

To see that it works, suppose that K=(0,0,1). Then, P = K x R will lie in the xy-plane, and, after normalization (call the normalized vector P'), on the equator of the unit sphere. P' is then entirely determined by its longitude. It is also clear that the direction of P and P', hence the longitude of P', depends only upon the longitude of R: If the longitude of R is θ, then the longitude of P' is θ+π/2 (mod 2π). But the longitude of R is uniformly distributed over the sphere, hence the longitude of P' is uniformly distributed over the equator. This means that P' is effectively a randomly chosen unitary vector in the xy-plane.

It is clear by rotational symmetry that the result must the be same if we take a random vector K on the unit sphere and the plane perpendicular to K (through the origin) instead of the xy-plane, which is what you wanted.

3. Aug 19, 2014

### Staff: Mentor

Hi Chandrasekhar, welcome to PF!

Since "perpendicular to vector K" basically defines a plane, you only need to generate one random number in the range $[0, 2\pi[$, which corresponds to the azimuthal angle of the vector P in that plane. You can use a generic uniform random number generator to get a number in the range $[0,1[$, and multiply it by $2\pi$ to get the desired angle.

You then need to translate that into actual coordinates. I'm not sure what the best method for that, but here is how I would have a first go at it. Find the rotational matrix that rotates the vector K such that it is aligned with the z axis. Generate the random angle $\theta$ and define a vector in the x-y plane with that angle (using the usual convention that $\theta = 0$ corresponds to the positive x axis, $\theta = \pi/2$ is the positive y axis, etc.). Use the inverse of the rotation matrix to rotate that vector P in the proper direction.

4. Aug 19, 2014

### chogg

FWIW, one easy way to generate a random point on the sphere is to take a random draw from a standard 3D Gaussian, and normalize it. The 3D Gaussian is isotropic; hence, all directions are equally likely.

The only time it doesn't work is if you get the origin. Technically, that's the single most likely value (the mode), too. :) But it doesn't matter (much) in practice, since it happens with probability 0 (or, with floating point on a computer, with probability really-really-small).

5. Aug 19, 2014

### Erland

That's good (I had a more complicated method i mind.)
In fact, we don't even need to normalize here, since we must normalize P = K x R anyway.

6. Aug 19, 2014

### chogg

Hah! Good point.

7. Aug 19, 2014

### Chandrasekhar

Thank you very much to all of you for the interesting comments!

Inspired by Dr Claude answer I found a very efficent way for doing what I needed.

First I do the cross product between K and one of the axis, for example the x-axis = [1,0,0] (but it's the same with any axis), and I obtain a temporary vector which is, by construction, perpendicular to K:

P_temp = K * x-axis ( * = cross product).

Then I normalize this vector and I use Rodrigues' formula (http://en.wikipedia.org/wiki/Rodrigues'_rotation_formula)
for rotate it around K with a random angle theta between 0 and 2*pie.

In this special case the second part of the formula is zero because of the dot product between K and P_temp.

It works and it is fast!
Thank yuo again!