Question
You are given a random-number generator that returns independent samples uniformly distributed on . Describe how to use this generator to return a two-dimensional point uniformly distributed over the square , and justify why the output distribution is uniform.
A mathematical construction or pseudocode is sufficient. You do not need to write a runnable script.
Hint 1
A linear map can shift and stretch an interval. Which map turns into ?
Hint 2
Give each coordinate its own generator call. To show the point is uniform over the square, check that every rectangle inside it gets probability proportional to its area.
How to Answer in the Interview
A five-minute phone-screen warm-up. The construction is one line, so the points are in the justification: why the point is uniform over the whole square.
Clarify first:
- "Can I call the generator twice per point?" The clean answer uses two calls. With only one call, see the follow-ups in Additional Notes.
TIP
How I'd say it in the interview: "I call the generator twice and map each draw with . Each coordinate is then uniform on . Because the two draws are independent, any rectangle gets probability equal to its area divided by , which is exactly the uniform distribution on the square."
Then walk through:
- Write the map
. - Show each coordinate is uniform: an interval
gets probability. - Multiply two interval probabilities into a rectangle probability using independence. Most candidates lose points here by stopping after step 2, but uniform coordinates alone do not make the point uniform.
- Conclude that probability equals area divided by
.
Solution
Draw two independent samples and return
Multiplying by stretches the interval to length , and subtracting shifts it to start at : maps to and to . The proof has two steps: each coordinate is uniform, and the pair spreads probability in proportion to area.
Step 1: each coordinate is uniform on
Take any interval with . Uniform on means its probability is its length divided by . To find which values of land there, add to every part of , then divide by :
Both endpoints lie inside , and a uniform lands in a subinterval of with probability equal to its length. That length is , which is exactly what uniformity requires. For example, needs , which has probability . Half the range gets half the probability.

The same calculation with shows is uniform on .
Step 2: independence turns intervals into area
Take a rectangle inside the square. The event depends only on , and the event depends only on . Because and are independent, so are these two events, and their joint probability is the product:

Every rectangle gets its share of the square's area , which is the definition of uniform on the square. Rectangle probabilities determine a two-dimensional distribution, because any region can be filled with small rectangles. The joint density is therefore the constant . Check with the upper-left quadrant : area , probability .
Code
A seeded sanity check, not a proof: the upper-left quadrant should hold of the points.
import random
def two_draws():
return 2 * random.random() - 1, 2 * random.random() - 1
def one_draw():
x = 2 * random.random() - 1
return x, x
random.seed(7)
n = 100_000
for name, sampler in [("two draws", two_draws), ("one draw", one_draw)]:
points = [sampler() for _ in range(n)]
share = sum(x < 0 < y for x, y in points) / n
print(f"{name}: upper-left share = {share:.3f}")
Output:
two draws: upper-left share = 0.251
one draw: upper-left share = 0.000
Additional Notes
Common mistakes
- Reusing one draw for both coordinates,
. Each coordinate is uniform, so a histogram of either looks right, but every point sits on the diagonal. The upper-left quadrant coversof the square and gets probability. - Getting the map wrong, for example
, which covers, or, which covers. Plug inandbefore moving on. - Calling a scatter plot that looks even a proof. A plot is a sanity check.
What this question tests
The question tests whether you separate the distribution of each coordinate from their independence, and turn both into an area argument. Likely follow-ups:
- Only one generator call per point. Read the odd-position bits of
as one number and the even-position bits as another. In exact arithmetic these are two independent uniforms. The cost is precision: a 53-bit float leaves about 26 bits per coordinate. - Sample uniformly in the unit disk. Rejection from the square accepts with probability
, abouttries per point. Polar sampling needs radius, because a uniform radius crowds points near the center. - Sample uniformly in the triangle under
with. Draw, and if, return. The flip maps the upper triangle onto the lower one without stretching it, so no draw is wasted.
Patterns to reuse
- The map
turnsinto. - To prove a point is uniform over a region, check that every rectangle gets probability proportional to its area.
- Functions of independent variables stay independent, so coordinate-wise transforms keep independence.