Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Friday, Dec 4.Claim the deal

Question

You are given a random-number generator that returns independent samples uniformly distributed on (0,1)(0,1). Describe how to use this generator to return a two-dimensional point uniformly distributed over the square (−1,1)×(−1,1)(-1,1)\times(-1,1), 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 (0,1)(0,1) into (−1,1)(-1,1)?

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 2U−12U-1. Each coordinate is then uniform on (−1,1)(-1,1). Because the two draws are independent, any rectangle gets probability equal to its area divided by 44, which is exactly the uniform distribution on the square."

Then walk through:

  1. Write the map (X,Y)=(2U−1,  2V−1)(X,Y)=(2U-1,\;2V-1).
  2. Show each coordinate is uniform: an interval (a,b)(a,b) gets probability (b−a)/2(b-a)/2.
  3. 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.
  4. Conclude that probability equals area divided by 44.

Solution

Draw two independent samples U,V∼Uniform⁡(0,1)U,V\sim\operatorname{Uniform}(0,1) and return

(X,Y)=(2U−1,  2V−1).(X,Y)=(2U-1,\;2V-1).

Multiplying by 22 stretches the interval to length 22, and subtracting 11 shifts it to start at −1-1: U=0U=0 maps to −1-1 and U=1U=1 to 11. 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 (−1,1)(-1,1)

Take any interval (a,b)(a,b) with −1<a<b<1-1<a<b<1. Uniform on (−1,1)(-1,1) means its probability is its length divided by 22. To find which values of UU land there, add 11 to every part of a<2U−1<ba<2U-1<b, then divide by 22:

a<2U−1<b  ⟺  a+12<U<b+12.a<2U-1<b \iff \frac{a+1}{2}<U<\frac{b+1}{2}.

Both endpoints lie inside (0,1)(0,1), and a uniform UU lands in a subinterval of (0,1)(0,1) with probability equal to its length. That length is (b+1)/2−(a+1)/2=(b−a)/2(b+1)/2-(a+1)/2=(b-a)/2, which is exactly what uniformity requires. For example, X∈(0,1)X\in(0,1) needs U∈(1/2,1)U\in(1/2,1), which has probability 1/21/2. Half the range gets half the probability.

Reversing a<X<b gives (a+1)/2<U<(b+1)/2. Its length is (b-a)/2, proving X is uniform on (-1,1).

The same calculation with VV shows YY is uniform on (−1,1)(-1,1).

Step 2: independence turns intervals into area

Take a rectangle R=(a,b)×(c,d)R=(a,b)\times(c,d) inside the square. The event a<X<ba<X<b depends only on UU, and the event c<Y<dc<Y<d depends only on VV. Because UU and VV are independent, so are these two events, and their joint probability is the product:

P((X,Y)∈R)=P(a<X<b) P(c<Y<d)=b−a2⋅d−c2=area⁡(R)4.P\bigl((X,Y)\in R\bigr)=P(a<X<b)\,P(c<Y<d)=\frac{b-a}{2}\cdot\frac{d-c}{2}=\frac{\operatorname{area}(R)}{4}.

Independence gives rectangle probability ((b-a)/2)((d-c)/2), equal to the rectangle's area divided by 4. The sketch shows that rectangle inside the square.

Every rectangle gets its share of the square's area 44, 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 1/41/4. Check with the upper-left quadrant (−1,0)×(0,1)(-1,0)\times(0,1): area 11, probability (1/2)(1/2)=1/4(1/2)(1/2)=1/4.

Code

A seeded sanity check, not a proof: the upper-left quadrant should hold 0.250.25 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, (2U−1,  2U−1)(2U-1,\;2U-1). Each coordinate is uniform, so a histogram of either looks right, but every point sits on the diagonal y=xy=x. The upper-left quadrant covers 1/41/4 of the square and gets probability 00.
  • Getting the map wrong, for example 2U2U, which covers (0,2)(0,2), or U−1U-1, which covers (−1,0)(-1,0). Plug in U=0U=0 and U=1U=1 before 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 UU 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 π/4\pi/4, about 1.271.27 tries per point. Polar sampling needs radius U\sqrt{U}, because a uniform radius crowds points near the center.
  • Sample uniformly in the triangle under x+y≤1x+y\le1 with x,y≥0x,y\ge0. Draw (U,V)(U,V), and if U+V>1U+V>1, return (1−U,1−V)(1-U,1-V). The flip maps the upper triangle onto the lower one without stretching it, so no draw is wasted.

Patterns to reuse

  • The map a+(b−a)Ua+(b-a)U turns Uniform⁡(0,1)\operatorname{Uniform}(0,1) into Uniform⁡(a,b)\operatorname{Uniform}(a,b).
  • 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.
Your progressLogin to save progress
Spot an issue or have a suggestion?