Generate Random Point in a Circle — Day 98(Python)

Photo by Nadine Shaabana on Unsplash

Today’s question is a Math based question. It is taken from the Daily leetcode coding challenge- March edition. Let us look into the problem statement.

478. Generate Random Point in a Circle

Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.


  1. input and output values are in floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to be in the circle.
  4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:

Example 2:

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren't any.

As an input, we have 2 co-ordinates i.e X and Y. These 2 input values are the coordinates for the center. We have another input which is the radius of the circle. We need to generate random points which fall within the circle.

Since we need to generate random uniform numbers, we will be using random.uniform function in Python. We require 2 parameters i.e min value and max value, that will act as input to random.uniform(). How can we find the min and max values for a circle?

Since we have a center coordinate and radius given, we will leverage this value to find the min and max values.

Once we find the min and max values, we will use random.uniform() to generate uniform coordinates.

One check to be done is to check if the values fall within the circle. We have a mathematical formula that checks if a point falls within the circle.

Source: Khan Academy

Let us look into the code snippet.

Complexity analysis.

Time Complexity

On average the time complexity is O(1) since we are expecting random to generate values within the circle. In the worst case, this can go up to infinity too.

Space Complexity

The space complexity is O(1) since we are not using any other data structure to store calculations.

Software Engineer. Find me @

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store