2

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.

Chris Godsil
  • 13,703
AkBKukU
  • 23
  • Why 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 or in Numerical Recipes, CH 7 for such algorithms to figure out how to map them to a specified range. – Amzoti Dec 18 '12 at 22:43
  • 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. – Steven Stadnicki Dec 18 '12 at 22:48
  • Well 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 – Amzoti Dec 18 '12 at 22:54
  • Possible 2D Duplicate of: http://math.stackexchange.com/questions/91109/get-random-x-y-z-point-inside-a-sphere – Frenzy Li Dec 19 '12 at 01:58

2 Answers2

3

Compare $x^2+y^2$ with $r^2$ and reject / retry if $x^2+y^2\ge r^2$.

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.

K2xL
  • 123