How can you generate random numbers and arrays using NumPy?

Quick Overview

Asks how to generate random numbers and arrays with NumPy, from scalars to multi-dimensional arrays. Tests the random Generator API, seeding for reproducibility, common distributions, sampling with or without replacement, and shuffling.

How can you generate random numbers and arrays using NumPy?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How can you generate random numbers and arrays using NumPy? Cover scalars and multi-dimensional arrays, the common distributions, sampling and shuffling, and how to make the results reproducible. ```hint Start from the state Think about which object holds the random state in current NumPy, how you create it, and what a seed does to it. Then look at how a single argument controls the shape of what comes back. ``` ### What a Strong Answer Covers - Creating a random generator with `np.random.default_rng` and what the seed controls - Methods for uniform floats, integers and common distributions, including which endpoints are included - How the `size` argument returns arrays of any shape in one call - Sampling with or without replacement, weighted sampling, and in-place shuffling versus returning a permuted copy - How the modern generator API differs from the legacy module-level `np.random` functions ### Follow-up Questions - Why is a `Generator` preferred over `np.random.seed` plus the module-level functions? - How would you give each of several parallel workers its own independent random stream? - How would you draw samples from a distribution NumPy does not provide, given its inverse CDF? - What is the difference between `rng.shuffle`, `rng.permutation` and `rng.permuted`?

Overview: Asks how to generate random numbers and arrays with NumPy, from scalars to multi-dimensional arrays. Tests the random Generator API, seeding for reproducibility, common distributions, sampling with or without replacement, and shuffling.

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

How can you generate random numbers and arrays using NumPy?

Cover scalars and multi-dimensional arrays, the common distributions, sampling and shuffling, and how to make the results reproducible.

What a Strong Answer Covers Guidance

  • Creating a random generator with np.random.default_rng and what the seed controls
  • Methods for uniform floats, integers and common distributions, including which endpoints are included
  • How the size argument returns arrays of any shape in one call
  • Sampling with or without replacement, weighted sampling, and in-place shuffling versus returning a permuted copy
  • How the modern generator API differs from the legacy module-level np.random functions

Follow-up Questions Guidance

  • Why is a Generator preferred over np.random.seed plus the module-level functions?
  • How would you give each of several parallel workers its own independent random stream?
  • How would you draw samples from a distribution NumPy does not provide, given its inverse CDF?
  • What is the difference between rng.shuffle , rng.permutation and rng.permuted ?