Reproducible Random Integer Sampling With NumPy default_rng

Quick Overview

Write a NumPy function that draws a reproducible array of uniform random integers from a half-open range using a freshly seeded default_rng generator. It tests the Generator API, seeding versus global random state, exclusive upper bounds, and reproducible random streams.

Reproducible Random Integer Sampling With NumPy default_rng

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `draw_integers(seed, low, high, size)` that returns an `np.ndarray` of `size` random integers drawn uniformly from `[low, high)`. The function must: - Create a fresh generator with `np.random.default_rng(seed)`. - Draw via `rng.integers(low, high, size)` (`high` exclusive). - Return an identical array for identical arguments, every time. ```python draw_integers(seed=42, low=0, high=10, size=5) # array([0, 7, 6, 4, 4]) # deterministic for seed=42 ``` ```hint Where the state lives Think about what could make two calls with the same arguments disagree: where the generator is created, what it is seeded with, and whether anything else can advance it. Also compare what `high` means here with Python's `random.randint`. ``` ### What a Strong Answer Covers - Creating and seeding a new `Generator` inside the function, with no global random state. - The half-open interval `[low, high)` and how it differs from an inclusive bound. - Why identical arguments give identical output, and what would break that. - Edge cases in the arguments, such as `low >= high` and an empty or multi-dimensional `size`. ### Follow-up Questions - Why prefer `default_rng` over `np.random.seed`? - How would you make the draws inclusive of `high`? - How would you give several parallel workers independent but reproducible streams from one seed?

Overview: Write a NumPy function that draws a reproducible array of uniform random integers from a half-open range using a freshly seeded default_rng generator. It tests the Generator API, seeding versus global random state, exclusive upper bounds, and reproducible random streams.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write draw_integers(seed, low, high, size) that returns an np.ndarray of size random integers drawn uniformly from [low, high).

The function must:

  • Create a fresh generator with np.random.default_rng(seed) .
  • Draw via rng.integers(low, high, size) ( high exclusive).
  • Return an identical array for identical arguments, every time.
draw_integers(seed=42, low=0, high=10, size=5)
# array([0, 7, 6, 4, 4])  # deterministic for seed=42

What a Strong Answer Covers Guidance

  • Creating and seeding a new Generator inside the function, with no global random state.
  • The half-open interval [low, high) and how it differs from an inclusive bound.
  • Why identical arguments give identical output, and what would break that.
  • Edge cases in the arguments, such as low >= high and an empty or multi-dimensional size .

Follow-up Questions Guidance

  • Why prefer default_rng over np.random.seed ?
  • How would you make the draws inclusive of high ?
  • How would you give several parallel workers independent but reproducible streams from one seed?