Find N random points within a Circle | C ++ program for the above approach ; Return a random double between 0 & 1 ; Function to find the N random points on the given circle ; Result vector ; Get Angle in radians ; Get length from center ; Add point to results . ; Return the N points ; Function to display the content of the vector A ; Iterate over A ; Print the N random points stored ; Driver Code ; Given dimensions ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define PI  3.141592653589 NEW_LINE double uniform ( ) { return ( double ) rand ( ) / RAND_MAX ; } vector < pair < double , double > > randPoint ( int r , int x , int y , int n ) { vector < pair < double , double > > res ; for ( int i = 0 ; i < n ; i ++ ) { double theta = 2 * PI * uniform ( ) ; double len = sqrt ( uniform ( ) ) * r ; res . push_back ( { x + len * cos ( theta ) , y + len * sin ( theta ) } ) ; } return res ; } void printVector ( vector < pair < double , double > > A ) { for ( pair < double , double > P : A ) { printf ( " ( % . 2lf , ▁ % .2lf ) STRNEWLINE " , P . first , P . second ) ; } } int main ( ) { int R = 12 ; int X = 3 ; int Y = 3 ; int N = 5 ; printVector ( randPoint ( R , X , Y , N ) ) ; return 0 ; }