# 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

The example I provided works fine, so long as it's implemented properly.

-
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
Siyfion: It seems that you were absent from the site since asking the question. I have upvoted this answer to (hopefully) prevent the server bumping it every now and then, however if you visit this site and see this comment, you should accept your own answer to ensure that. – Asaf Karagila May 23 '11 at 15:47
Thanks, I wanted to accept my own answer when I wrote it, but I had to wait 24hrs, and then I forgot! lol. – Siyfion May 25 '11 at 8:59