NumPy · Chapter 15 of 18

Random numbers and simulation

By the end of this chapter you can make a Generator with np.random.default_rng and a seed, say why the seed matters, draw numbers with random, integers, uniform, normal and choice (with and without replacement, with p=), reorder arrays with permutation and shuffle, recognise the old np.random.seed style, and use random numbers to answer questions: a Monte Carlo simulation with its P10, P50 and P90, and a bootstrap interval. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Analysis
  • 4 tasks
  • NumPy
  • In the all labs plan

The first 2 chapters of NumPy are free. This one comes with the all labs plan.

Why you need this

Some of DhoopGrid's questions have no single answer in the data. Meenal must tell the bank what October may earn, and October has not happened: it depends on how many cloudy days it brings. Bhanwar checks 24 panels a week, and they must be picked fairly, so no panel is skipped for months, and Rukmini wants to be able to repeat any week's draw. Imran measured 30 panels at noon and wants to know how far their mean might be from the plant's.

You cannot list every possible October with a loop over cases. What you can do is simulate many Octobers with random cloudy days, and look at the spread of the results. With NumPy, 10,000 simulated months are one array of shape (10000, 31), made and summed without a Python loop.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    inspection.py: a fair inspection sample

    Write inspection_sample(ids, seed) in inspection.py. ids is an array of panel id strings and seed a whole number. Make a Generator with np.random.default_rng(seed), then call its choice method once: from ids, size 24, without replacement (replace=False). Return what it gives: an array of 24 id strings with no id twice, the same 24 every time for the same seed.

    The Check uses the 2,400 ids with seeds 7 and 2026 and compares with exactly that draw, so make no other draws from the Generator before it, and use the seed you are given.

  2. Task 2

    month_revenue.py: October's revenue, simulated

    Write simulate_month(clear_day_rs, rng, n) in month_revenue.py. rng is a NumPy Generator: draw every random number from it. Simulate n Octobers of 31 days. Each day is cloudy with probability 1/6 and then earns a share of clear_day_rs drawn uniformly between 0.4 and 0.8; otherwise it is clear and earns a share drawn uniformly between 0.92 and 1.0. A month's revenue is the sum of its 31 days. Return an array of 3 floats: the 10th, 50th and 90th percentiles (P10, P50, P90) of the n months' revenues, in rupees, lowest first.

    The Check calls simulate_month(28000.0, rng, 10000) and simulate_month(21500.0, rng, 2000) and accepts answers within 1 % and 2 % of its own (random answers differ a little). It also calls it twice with Generators made from the same seed (the answers must be equal) and with two different seeds (they must differ). Use no Python loop: a for, a while or a comprehension in simulate_month fails the task; draw arrays of shape (n, 31).

  3. Task 3

    bootstrap.py: how sure is the mean?

    Write bootstrap_interval(values, rng, n) in bootstrap.py. values is a 1-D array of readings in watts, rng a NumPy Generator and n how many new samples to draw. Each new sample has as many readings as values, drawn from values with replacement. Return an array of 2 floats: the 2.5th and the 97.5th percentiles of the n samples' means, a 95 % interval for the mean, lowest first.

    The Check uses n of 5,000 on Bhanwar's 30 noon readings and on 20 readings of row 12, and accepts answers within 2 W of its own. It also calls it twice with Generators made from the same seed (equal answers) and with two different seeds. Use no Python loop: a for, a while or a comprehension in bootstrap_interval fails the task; draw all n samples as one array of shape (n, number of readings).

  4. Task 4 · interview question

    Interview: seeded.py, np.random.seed

    sensor_noise(n, seed) in seeded.py makes made-up sensor noise for Imran's checks with np.random.seed(seed) and np.random.normal. Since it was added, the program's other random numbers, drawn later from NumPy's global functions, are the same on every run. Rewrite it with a Generator: make one with np.random.default_rng(seed), then draw with its normal method, mean 0, standard deviation 2.0, size n, and return that array of n floats. It must not touch NumPy's global random state.

    The Check tries n of 5 with seed 42 and n of 1,000 with seed 7, compares with exactly that draw, and checks that the global random numbers drawn after your function are the ones they would have been without it.

    Imran will ask: why default_rng with a seed, and not np.random.seed?

What you will learn

  • np.random.default_rng(seed): a Generator, the object every random number comes from.
  • Why a seed: the same numbers on every run, for checks and for reports someone must repeat.
  • random, integers, uniform, normal: the draws you will use most.
  • choice: picking from an array, with or without replacement, with p= for unequal chances; permutation and shuffle.
  • The old style: np.random.seed and np.random.rand, to recognise.
  • Monte Carlo: many random runs, then percentiles (P10, P50, P90).
  • The bootstrap: how much a statistic of a sample may vary.

More in Analysis

Build "Random numbers and simulation" yourself

DhoopGrid runs a solar plant in the Thar desert with 2,400 panels. You turn its raw readings into answers. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of NumPy are free. This one comes with the all labs plan.