Random points inside a parallelogram
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 random points inside a parallelogram is easier than it looks because a parallelogram is an affine transformation of the unit square. If you can sample uniformly from two independent numbers between zero and one, you can map those numbers directly into the parallelogram and keep the distribution uniform.
Represent the Parallelogram with Two Edge Vectors
Start with one corner of the parallelogram, call it origin, and two edge vectors a and b. Every point inside the shape can be written as:
point = origin + u * a + v * b
where u and v are both in the interval from 0 to 1.
This is the key fact. A parallelogram is not like a triangle, so you do not need to fold or swap random numbers. You just sample u and v independently and map them through the two basis vectors.
Sample Uniformly from the Unit Square
The simplest Python implementation looks like this:
Because u and v are uniform on the unit square, the mapped points are uniform over the area of the parallelogram. Affine transformations preserve uniform density up to a constant scaling factor, and that scaling factor is exactly the area distortion from the square to the parallelogram.
Generate Many Points at Once
If you need a large sample for simulation or visualization, NumPy makes the operation compact and fast:
This version is useful for Monte Carlo integration, graphics sampling, or geometry tests where you need thousands of points rather than one point at a time.
Why the Triangle Trick Does Not Belong Here
People often confuse parallelogram sampling with triangle sampling. For a triangle, there is a well-known method where you reflect or swap coordinates when their sum crosses one. That is necessary because a triangle occupies only half of a square-like parameter region.
A full parallelogram does not need that correction. The entire u, v square maps directly into the full shape. If you apply the triangle trick here, you will bias the distribution and undersample part of the parallelogram.
That distinction is important because the wrong method may still "look random" at a glance while being mathematically incorrect.
Handle Degenerate Cases
This method assumes a and b are not collinear. If they are parallel, the parallelogram collapses into a line segment or a point, and uniform area sampling no longer makes sense.
You can detect that by checking whether the 2D cross product is close to zero:
A nonzero result means the vectors span area and the construction is valid.
Common Pitfalls
The most common mistake is using the triangle-sampling reflection trick for a parallelogram. That changes the distribution and is simply the wrong geometry.
Another issue is forgetting that the two edge vectors must originate from the same corner. If a and b do not describe the parallelogram from one shared origin, the formula does not describe the intended region.
People also sometimes ignore degenerate input where the edge vectors are parallel. In that case, the "parallelogram" has no interior area to sample.
Summary
- A parallelogram can be sampled uniformly by mapping two independent uniform numbers through its edge vectors.
- The correct formula is
origin + u * a + v * bwithuandvin the interval from0to1. - This is uniform because a parallelogram is an affine image of the unit square.
- Do not use the triangle reflection trick for a full parallelogram.
- Check that the two edge vectors are not collinear before treating the shape as a valid area.
Related reading
- Random walk around a central location in a limited area?
- Random weighted choice
- Randomly Generate Letters According to their Frequency of Use?
- Randomly selecting k different numbers in a range
- Rasterizing a 2D polygon
- Ray-box Intersection Theory
- Ray-triangle intersection
- Ray - Octree intersection algorithms

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.