Algorithm Calculate pseudo-random point inside an ellipse
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Generating pseudo-random points within an ellipse is a common task in simulation, computer graphics, and optimization. Unlike a circle, where a simple transformation of polar coordinates can suffice, the ellipse's asymmetry requires a more nuanced approach. This article explores the algorithms and mathematics behind this process, while also providing a practical example and key references.
Mathematical Background
An ellipse is defined by its two axes: the semi-major axis and the semi-minor axis . Its equation in Cartesian coordinates centered at the origin is given by:
To generate a random point within this geometric shape, we need a strategy that respects its bounded area while maintaining uniform distribution.
Algorithmic Approach
Transformation Method
- Generate a Random Point in a Unit Disk: • Start by generating a random point in a unit circle of radius 1. This can be efficiently done using polar coordinates: • Choose a random radius , where is a uniform random number between 0 and 1. This ensures the distribution remains uniform within the circle. • Choose an angle uniformly between 0 and . • Convert to Cartesian coordinates: , .
- Transform to the Elliptical Space: • Scale the coordinates to fit the ellipse: • •
The transformed point will be uniformly distributed within the ellipse defined by parameters and .
Box Rejection Sampling
An alternative method to generate random points in an ellipse involves bounding the ellipse within a rectangle and using rejection sampling:
- Bounding Box: • Define a bounding rectangle with sides and centered at the origin.
- Sampling and Acceptance: • Uniformly sample a point within this bounding box. • Check if this point satisfies the ellipse equation: . • If yes, accept this point; otherwise, reject it and resample.
While this method is simple to implement, it may be inefficient if the ellipse is significantly elongated in one direction.
Practical Example
• Computer Graphics: For creating naturally random distributions in particle systems or texture generation. • Physics Simulations: To model celestial bodies or other phenomena where elliptical distributions are natural. • Statistics: In generating data points for elliptical distributions or confidence region plotting.
Related reading
- Algorithm C/C Fastest way to compute 2nd with a n and d 32 or 64 bit integers
- Algorithm challenge Generate color scheme from an image
- Algorithm Complexity Security MD5 or SHA1?
- Algorithm Create color from string
- algorithm for a random space bordered by elements of equal length
- Algorithm for Calculating Binomial Coefficient
- Algorithm design can you provide a solution to the multiple knapsack problem?
- Algorithm design to assign nodes to graphs

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.