Seeded Random Assignment of Units to Treatment and Control with NumPy

Quick Overview

Write a NumPy function that assigns anonymous units to treatment or control from a seed with a configurable treatment probability. It tests reproducible random number generation, threshold edge cases, and when seeded draws should give way to hashing a stable user ID.

Seeded Random Assignment of Units to Treatment and Control with NumPy

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `def random_assign(n: int, seed: int, treat_prob: float = 0.5) -> np.ndarray` to assign `n` anonymous units to treatment (1) or control (0) using seeded random assignment. Return `(draws < treat_prob).astype(np.int64)` where `draws = rng.random(n)`. - Construct the generator as `rng = np.random.default_rng(seed)`, not with the legacy `np.random.seed` API. - Make exactly one call `rng.random(n)` (no loop of `rng.random()`), with no other `rng` method called before it. - A unit is treated when its draw is strictly less than `treat_prob`. - Return shape `(n,)` and dtype `int64`. - Two calls with the same `seed` return identical arrays. Examples: ```python a = random_assign(5, seed=42) a.shape, a.dtype # ((5,), dtype('int64')), every value is 0 or 1 np.array_equal(random_assign(5, 42), random_assign(5, 42)) # True random_assign(4, seed=0, treat_prob=0.0) # array([0, 0, 0, 0]) random_assign(4, seed=0, treat_prob=1.0) # array([1, 1, 1, 1]) ``` ```hint Boundary values Check what your comparison does at `treat_prob = 0.0` and `treat_prob = 1.0`, given the interval `rng.random` draws from. ``` ### Constraints & Assumptions - `n >= 0`, and `treat_prob` lies in `[0, 1]`. - Units have no stable ID to hash, so the seed and the order of the units fully determine the assignment. ### What a Strong Answer Covers - Building a dedicated generator from the seed and making exactly one vectorized draw - The strict `<` comparison and its behavior at `treat_prob` equal to 0 and 1 - Returning an `int64` array of shape `(n,)`, including the empty case - Why the same seed reproduces the same assignment, and which changes would break that ### Follow-up Questions - Why use `np.random.default_rng(seed)` instead of `np.random.seed(seed)` in shared code? - This gives each unit an independent coin flip, so group sizes vary. How would you guarantee exactly `round(n * treat_prob)` treated units? - When would you switch to hash-based assignment on a stable user ID? - How would you check that the realized split is consistent with `treat_prob`?

Overview: Write a NumPy function that assigns anonymous units to treatment or control from a seed with a configurable treatment probability. It tests reproducible random number generation, threshold edge cases, and when seeded draws should give way to hashing a stable user ID.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement def random_assign(n: int, seed: int, treat_prob: float = 0.5) -> np.ndarray to assign n anonymous units to treatment (1) or control (0) using seeded random assignment. Return (draws < treat_prob).astype(np.int64) where draws = rng.random(n).

  • Construct the generator as rng = np.random.default_rng(seed) , not with the legacy np.random.seed API.
  • Make exactly one call rng.random(n) (no loop of rng.random() ), with no other rng method called before it.
  • A unit is treated when its draw is strictly less than treat_prob .
  • Return shape (n,) and dtype int64 .
  • Two calls with the same seed return identical arrays.

Examples:

a = random_assign(5, seed=42)
a.shape, a.dtype
# ((5,), dtype('int64')), every value is 0 or 1

np.array_equal(random_assign(5, 42), random_assign(5, 42))
# True

random_assign(4, seed=0, treat_prob=0.0)
# array([0, 0, 0, 0])

random_assign(4, seed=0, treat_prob=1.0)
# array([1, 1, 1, 1])

Constraints & Assumptions

  • n >= 0 , and treat_prob lies in [0, 1] .
  • Units have no stable ID to hash, so the seed and the order of the units fully determine the assignment.

What a Strong Answer Covers Guidance

  • Building a dedicated generator from the seed and making exactly one vectorized draw
  • The strict < comparison and its behavior at treat_prob equal to 0 and 1
  • Returning an int64 array of shape (n,) , including the empty case
  • Why the same seed reproduces the same assignment, and which changes would break that

Follow-up Questions Guidance

  • Why use np.random.default_rng(seed) instead of np.random.seed(seed) in shared code?
  • This gives each unit an independent coin flip, so group sizes vary. How would you guarantee exactly round(n * treat_prob) treated units?
  • When would you switch to hash-based assignment on a stable user ID?
  • How would you check that the realized split is consistent with treat_prob ?