Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random points in a circular distribution

I am wondering how i could generate random numbers that appear in a circular distribution.

I am able to generate random points in a rectangular distribution such that the points are generated within the square of (0 <= x < 1000, 0 <= y < 1000):

How would i go upon to generate the points within a circle such that:

(x−500)^2 + (y−500)^2 < 250000 ?

like image 932
Jack8246 Avatar asked Jun 01 '15 00:06

Jack8246


People also ask

How do you generate random points in Python?

Python provides a random module to generate random numbers. To generate random numbers we have used the random function along with the use of the random. randint function. randint accepts two parameters, a starting point, and an ending point.

How do you generate random points in a polygon?

The first step is to decompose the polygon into triangles. You can use the relative areas of the triangles to determine the probability that a random point is in each triangle. Finally, you can generate random points in the union of the triangles.


1 Answers

import random
import math

# radius of the circle
circle_r = 10
# center of the circle (x, y)
circle_x = 5
circle_y = 7

# random angle
alpha = 2 * math.pi * random.random()
# random radius
r = circle_r * math.sqrt(random.random())
# calculating coordinates
x = r * math.cos(alpha) + circle_x
y = r * math.sin(alpha) + circle_y

print("Random point", (x, y))

In your example circle_x is 500 as circle_y is. circle_r is 500.

Another version of calculating radius to get uniformly distributed points, based on this answer

u = random.random() + random.random()
r = circle_r * (2 - u if u > 1 else u)
like image 186
f43d65 Avatar answered Sep 19 '22 05:09

f43d65