Find N random points within a Circle - GeeksforGeeks

07 Feb, 2022
Given four integers N, R, X, and Y such that it represents a circle of radius R with [X, Y] as coordinates of the center. The task is to find N random points inside or on the circle. Examples:
Input: R = 12, X = 3, Y = 3, N = 5 Output: (7.05, -3.36) (5.21, -7.49) (7.53, 0.19) (-2.37, 12.05) (1.45, 11.80)Input: R = 5, X = 1, Y = 1, N = 3 Output: (4.75, 1.03) (2.57, 5.21) (-1.98, -0.76)
Approach: To find a random point in or on a circle we need two components, an angle(theta) and distance(D) from the center. After that Now, the point (xi, yi) can be expressed as:
xi = X + D * cos(theta)
yi = Y + D * sin(theta)
Below is the implementation of the above approach:
C++
Java
Python3
C#
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;#define PI 3.141592653589 // Return a random double between 0 & 1double uniform(){    return (double)rand() / RAND_MAX;} // Function to find the N random points on// the given circlevector<pair<double, double> > randPoint(    int r, int x, int y, int n){    // Result vector    vector<pair<double, double> > res;     for (int i = 0; i < n; i++) {         // Get Angle in radians        double theta = 2 * PI * uniform();         // Get length from center        double len = sqrt(uniform()) * r;         // Add point to results.        res.push_back({ x + len * cos(theta),                        y + len * sin(theta) });    }     // Return the N points    return res;} // Function to display the content of// the vector Avoid printVector(    vector<pair<double, double> > A){     // Iterate over A    for (pair<double, double> P : A) {         // Print the N random points stored        printf("(%.2lf, %.2lf)\n",               P.first, P.second);    }} // Driver Codeint main(){    // Given dimensions    int R = 12;    int X = 3;    int Y = 3;    int N = 5;     // Function Call    printVector(randPoint(R, X, Y, N));    return 0;}
// Java program for the above approachimport java.util.*; class GFG{     static final double PI = 3.141592653589;static class pair{    double first, second;     public pair(double first,                double second)    {        super();        this.first = first;        this.second = second;    }} // Return a random double between 0 & 1static double uniform(){return Math.random();} // Function to find the N random points on// the given circlestatic Vector<pair> randPoint(int r, int x,                              int y, int n){         // Result vector    Vector<pair> res = new Vector<pair>();     for(int i = 0; i < n; i++)    {                 // Get Angle in radians        double theta = 2 * PI * uniform();         // Get length from center        double len = Math.sqrt(uniform()) * r;         // Add point to results.        res.add(new pair(x + len * Math.cos(theta),                         y + len * Math.sin(theta)));    }         // Return the N points    return res;} // Function to display the content of// the vector Astatic void printVector(Vector<pair> A){     // Iterate over A    for(pair P : A)    {                 // Print the N random points stored        System.out.printf("(%.2f, %.2f)\n",                          P.first, P.second);    }} // Driver Codepublic static void main(String[] args){         // Given dimensions    int R = 12;    int X = 3;    int Y = 3;    int N = 5;     // Function call    printVector(randPoint(R, X, Y, N));}} // This code is contributed by Rajput-Ji
# Python program for the above approachimport mathimport randomPI = 3.141592653589; class pair:     def __init__(self, first, second):        self.first = first;        self.second = second; # Return a random between 0 & 1def uniform():    return random.random(); # Function to find the N random points on# the given circledef randPoint(r, x, y, n):       # Result vector    res = list();     for i in range(n):        # Get Angle in radians        theta = 2 * PI * uniform();         # Get length from center        len = math.sqrt(uniform()) * r;         # Add point to results.        res.append(pair((x + len * math.cos(theta)), (y + len * math.sin(theta))));     # Return the N points    return res; # Function to display the content of# the vector Adef printVector(A):     # Iterate over A    for P in A:        # Print the N random points stored        print("({0:.2f}".format(P.first),", ","{0:.2f})".format(P.second)); # Driver Codeif __name__ == '__main__':    # Given dimensions    R = 12;    X = 3;    Y = 3;    N = 5;     # Function call    printVector(randPoint(R, X, Y, N)); # This code is contributed by 29AjayKumar
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{     static readonly double PI = 3.141592653589;class pair{    public double first, second;     public pair(double first,                double second)    {        this.first = first;        this.second = second;    }} // Return a random double between 0 & 1static double uniform(){    return new Random().NextDouble();} // Function to find the N random points on// the given circlestatic List<pair> randPoint(int r, int x,                              int y, int n){         // Result vector    List<pair> res = new List<pair>();    for(int i = 0; i < n; i++)    {                 // Get Angle in radians        double theta = 2 * PI * uniform();         // Get length from center        double len = Math.Sqrt(uniform()) * r;         // Add point to results.        res.Add(new pair(x + len * Math.Cos(theta),                         y + len * Math.Sin(theta)));    }         // Return the N points    return res;} // Function to display the content of// the vector Astatic void printList(List<pair> A){     // Iterate over A    foreach(pair P in A)    {                 // Print the N random points stored        Console.Write("({0:F2}, {1:F2})\n",                          P.first, P.second);    }} // Driver Codepublic static void Main(String[] args){         // Given dimensions    int R = 12;    int X = 3;    int Y = 3;    int N = 5;     // Function call    printList(randPoint(R, X, Y, N));}} // This code is contributed by 29AjayKumar
(7.05, -3.36)
(5.21, -7.49)
(7.53, 0.19)
(-2.37, 12.05)
(1.45, 11.80)
Time Complexity: O(N) Space Complexity: O(N) 
Rajput-Ji
29AjayKumar
saurabh1990aror
surinderdawra388
circle
school-programming
Geometric
Randomized
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convex Hull using Divide and Conquer Algorithm
Equation of circle when three points on the circle are given
Circle and Lattice Points
Orientation of 3 ordered points
Program to find slope of a line
QuickSort using Random Pivoting
Shuffle a given array using Fisher–Yates shuffle Algorithm
K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
Reservoir Sampling
Estimating the value of Pi using Monte Carlo