3
$\begingroup$

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!

  • 6
    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).2011-05-19
  • 0
    I'll try that now, although I think the sin and cos functions in 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..)2011-05-19
  • 0
    Yep, this should definitely work.2011-05-19
  • 0
    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! ;)2011-05-19

1 Answers 1

2

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

  • 0
    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.2011-05-19
  • 0
    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.2011-05-23
  • 0
    Thanks, I wanted to accept my own answer when I wrote it, but I had to wait 24hrs, and then I forgot! lol.2011-05-25
  • 0
    I am not 100% sure I am correct but, just in case, I think that the random number has to be in the range `[0,1)`. Sine of Tau is.... something strange? (In C# I get `-2.449213E-16`)2018-10-17