I am generating two random numbers to choose a point in a circle randomly. The circles radius is 3000 with origin at the center. I'm using -3000 to 3000 as my bounds for the random numbers. I'm trying to get the coordinates to fall inside the circle (ie 3000, 3000 is not in the circle). What equation could I use to test the limits of the two numbers because I can generate a new one if it falls out of bounds.
Constrain Random Numbers to Inside a Circle
2
$\begingroup$
geometry
circles
-
0Why don't you have your random number generator generate numbers that are mapped to be inside the circle as to avoid this? For example, a strategy simlar to http://stackoverflow.com/questions/137783/expand-a-random-range-from-15-to-17. Also, you can look in [**Knuth's**](http://www-cs-faculty.stanford.edu/~uno/programs.html) or in **Numerical Recipes, CH 7** for such algorithms to figure out how to map them to a specified range. – 2012-12-18
-
1@Amzoti Actually, the first recommendation in Knuth for generating random points in a circle, IIRC, is the 'generate a new one' rejection sampling approach that the OP mentions - it sounds like they're just trying to figure out how to implement it. – 2012-12-18
-
0Well then, they can find that here: http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly (look at the last method for quick and dirty or one of the earilier methods with the nice pictures) Regards -A – 2012-12-18
-
0Possible 2D Duplicate of: http://math.stackexchange.com/questions/91109/get-random-x-y-z-point-inside-a-sphere – 2012-12-19
2 Answers
3
Compare $x^2+y^2$ with $r^2$ and reject / retry if $x^2+y^2\ge r^2$.
-
0This worked perfectly for me, thank you. – 2012-12-19
0
Another way. Choose a random angle between 0 and 2PI. Then choose a random number between 0 and r. pt(x,y) =
Note that it isn't uniform.