Random point generator.

Mr. Schrodinger recently developed a new hypothesis. For testing his hypothesis he needs some points inside a particular circle. For good testing, points should be uniformly distributed, i.e. evenly distributed inside the circle. So you as an assistant are asked to implement a random point generator function that will do the task.

More formally, you need to implement a function that will generate uniformly distributed random points inside the given circle.

Note:

A point on the circumference of a circle is considered an inner point.

Input Format:

The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains three space-separated integers ‘X’, ‘Y’, ‘R’ denoting x-coordinate, y-coordinate of the center of the circle and ‘R’ denoting the radius of the circle.

Output Format:

For each test case, the output will be 1, if you generated correct uniformly distributed points as described in the problem, else 0.

Note:

You do not need to input or print anything, and it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
-10 ^ 9 <= X, Y <= 10 ^ 9
1 <= R <= 10 ^ 9

Time Limit: 1sec
CodingNinjas
author
2y

Let y be the smallest integer such that 2^y >= N.

So, we will call G(y) till we get a number in the desired range.

Follow-up - Expected Number of calls to G before we get a number in the desired range.

CodingNinjas
author
2y
Rectangular coordinates

The idea is to generate a random x-coordinate and y-coordinate and if it lies inside the circle, then return this point.

 

Algorithm:

 

  • Let the x-coordinate and y-coordinate of the center of a given circle be ‘Xc’ and ‘Yc’ and radius of the circle be ‘Rc’.
  • Run an infinite loop.
    • Declare two double variables say ‘X’ and ‘Y’.
    • Set ‘X’ and ‘Y’ to (random double value between -1 to 1) * ‘Rc’, this will ensure that ‘X’ and ‘Y’ individually do not exceed the radius of the circle, but the point (‘X’, ‘Y’) may lie outside the circle.
    • If  ‘X’ ^ 2 + ‘Y’ ^ 2 <= ‘Rc’ ^ 2 - this condition shows that point ( ‘X’, ‘Y’) lies inside the circle.
      • Return { ‘X’, ‘Y’ }.
Space Complexity: O(1)Explanation:

O(1)

 

As we are not using any extra space.

Time Complexity: O(1)Explanation:

O(1)

 

Although we are using an Infinite loop but random function will generate the desired point in constant time. Hence the overall time complexity is constant.


C++ (g++ 5.4)
/*
Time Complexity: O(1)
Space Complexity: O(1)
*/

vector randomPoint(double radius, double xc, double yc)
{
// Declare two varaible to store random points inside circle.
double x, y;

while(true)
{
// Declare two varaibles to store a random value between -1 to 1.
double rand1,rand2;
rand1 = (2 * ((double)rand() / RAND_MAX) - 1.0);
rand2 = (2 * ((double)rand() / RAND_MAX) - 1.0);

x = rand1 * radius;
y = rand2 * radius;

// If the current point lies inside the circle, then break the loop.
if (x * x + y * y <= radius * radius)
{
break;
}

}

// Return point.
return { xc + x, yc + y };
}

Python (3.5)
'''
Time Complexity: O(1)
Space Complexity: O(1)
'''

import random

def randomPoint(radius, xc, yc):

# Declare two varaible to store random points inside circle.
x = 0
y = 0

while True:
# Declare two varaibles to store a random value between -1 to 1.
rand1 = (2 * (random.uniform(0,1)) - 1.0)
rand2 = (2 * (random.uniform(0,1)) - 1.0)

x = rand1 * radius
y = rand2 * radius

# If the current point lies inside the circle, then break the loop.
if (x * x + y * y <= radius * radius):
break

# Return point.
return [xc + x, yc + y]

Java (SE 1.8)
/*
Time Complexity: O(1)
Space Complexity: O(1)
*/

import java.util.ArrayList;

public class Solution {

public static ArrayList randomPoint(double radius, double xc, double yc) {
// Declare two varaible to store random points inside circle.
Double x, y;

while(true)
{
// Declare two varaibles to store a random value between -1 to 1.
Double rand1,rand2;
rand1 = (2 * (Double)Math.random() - 1.0);
rand2 = (2 * (Double)Math.random() - 1.0);

x = rand1 * radius;
y = rand2 * radius;

// If the current point lies inside the circle, then break the loop.
if (x * x + y * y <= radius * radius)
{
break;
}

}

ArrayList sol = new ArrayList();
sol.add(xc+x);
sol.add(yc+y);
// Return point.
return sol;
}
}
CodingNinjas
author
2y
Polar coordinates

Any point on a circle can be described as a ( 'R', 'THETA') where 'R' is the distance of a point from the center of the circle and 'THETA' is the angle with the x-axis. To generate a ...read more

Add answer anonymously...
Trilogy Innovations Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter