# Generating a random point on the unit circle

I'm trying to figure out a way to generate a random point on the unit circle in an application I am developing (I'm a programmer).

So far I have the following (in pseudo-code), where Z is a random number between 0.0 and 1.0:

theta = (2.0 * PI) * Z

2DVector.x = cos(theta)
2DVector.y = sin(theta)

result: 2DVector


I know that it's wrong, as I'm getting nothing but massive x values and tiny y values. But I'm not familiar enough with the unit circle mathematics to know where I'm going wrong!

-
This should definitely work. Are you sure that your sine and cosine functions take radians as arguments and not degrees? In other words: What happens if you modify the first line into theta = 360 * Z? Do you get the expected result? The reason why I'm asking is that $\cos$ is very close to $1$ for small angles (and you'd get angles between $0$ and $6.28...$ if the arguments are interpreted in degrees). –  t.b. May 19 '11 at 8:26
I'll try that now, although I think the sin and cos functions in <maths.h> take the angle in radians. I guess this points the finger more at my code, with a potential mistake somewhere else (either in the random float generation, or casting of variables..) –  Siyfion May 19 '11 at 8:36
Yep, this should definitely work. –  Eelvex May 19 '11 at 8:55
And in fact it does! It was a silly casting-error on my part in the code. So the above it a perfectly way of getting a random point on the unit circle! ;) –  Siyfion May 19 '11 at 9:44

Great! Glad to hear that. Intuitively, the thing is that $\theta \mapsto (\cos{(2\pi\theta)},\sin{2\pi\theta)})$ travels through the circle at constant speed as $\theta$ moves through $[0,1]$, so uniformly distributed $\theta \in [0,1]$ get uniformly distributed on the circle via your map in the code. –  t.b. May 19 '11 at 9:52