You're generating a random angle phi
uniformly between 0 and 2Pi, which is the "heading" of your vector. By using unscaled sin
and cos
, you're implicitly assuming that the length of the vector is 1 and decomposing it into its vertical and horizontal components. Looks basically correct to me.
A couple of minor details: 1) When you divide by RAND_MAX
rather than RAND_MAX+1
, you're allowing both zero and x to occur, in your case both 0 and 2Pi, and those two values are the same in angular terms. You may want to change the divisor accordingly. 2) I'd recommend multiplying by 2.0
rather than 2
to avoid the implicit up-conversion when you multiply it by M_PI
, but this is probably taken care of by your compiler anyway.
There's another way to do this which generalizes to an arbitrary number of dimensions: Generate k-tuples (z1, z2,..., zk) where each of the z's has a normal distribution. Calculate the norm N=sqrt(z12 + z22 + ... + zk2), and use it to create a normalized vector X=(z1/N, z2/N,..., zk/N). The result is that X has unit length (because of the normalization) and its direction is uniformly distributed over the k-dimensional unit hypersphere thanks to the distributional properties of the normals.