How can I generate random points on a circles circumference in javascript

Mouseroot picture Mouseroot · Mar 26, 2012 · Viewed 30.7k times · Source

I am trying to write a function that will randomly return an (x,y) co-ordinates around a given circumference so if I have a point that's at (0,0) (being the center of the div) how can I write a function that randomly places other entities that appear among the outer edge of a circle.

All I need is the equation i know it has something to do with getting the distance from the center to the circumference edge just no idea how to calculate it and randomize it so it looks good.

Answer

Niet the Dark Absol picture Niet the Dark Absol · Mar 26, 2012

Just get a random angle:

var angle = Math.random()*Math.PI*2;

Then

x = Math.cos(angle)*radius;
y = Math.sin(angle)*radius;

Done.