Generate Controlled Variation

Add generated noise to familiar measurements under an explicit shape and range contract, then check the properties every valid result must satisfy.

A deterministic measurement table always produces the same values. Some experiments need controlled variation as an explicit input. The program should state the generator, requested shape, and allowed range, then check the result before using it.

Create a Generator, Then Request Values

The recurring calibrated table is:

Its shape is (3, 2): axis 0 means observations and axis 1 means sensors. We will add one noise value to every table position. The contract is:

shape: (3, 2)
lower bound: -0.2, included
upper bound: 0.2, excluded

Create NumPy's current default random generator with default_rng:

rng = np.random.default_rng(2026)

The name rng stands for random number generator. The generator is a stateful, deterministic object: its current internal state determines the next values, and requesting values advances that state. The seed 2026 supplies a repeatable starting state under the same generator algorithm and call sequence. The next lesson examines that replay contract in detail.

Now request floating-point values from the half-open interval [-0.2, 0.2):

uniform fills the requested shape with values greater than or equal to the lower bound and less than the upper bound. This lesson uses that operation as a clear bounded-value contract; it does not yet make a claim about a probability distribution or a real sensor process.

Q1. Read a half-open generation interval

Which condition describes every value requested by rng.uniform(-0.2, 0.2, size=(3, 2))?

Choose one

Select one choice, then check.

HintTranslate the bracket notation

In [-0.2, 0.2), the square bracket includes -0.2; the parenthesis excludes 0.2.

SolutionUse the stated half-open bounds

Every generated value satisfies 0.2value<0.2-0.2 \leq value < 0.2. The requested shape and bounds are separate parts of the contract.

Not attempted
Review

Not marked done.

Check Properties Before Inspecting Individual Values

Generated values are output from a computation. Check the properties the program requires rather than copying one printed sequence into the lesson:

noise shape: (3, 2)
matches table: True
lower bound: True
upper bound: True
result shape: (3, 2)

These checks remain meaningful even when another valid run contains different numbers. The addition also preserves the measurement axes because noise and calibrated have the same shape.

To make the change visible, inspect one table position:

The exact noise value comes from the generator state. The final check verifies the relation among input, variation, and output at that position.

Q2. Generate and verify bounded noise

Complete the request so noise matches the calibrated table and stays in [-0.2, 0.2). Then add it to the table.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintCopy the contract into the call

Use rng.uniform(-0.2, 0.2, size=calibrated.shape), then set noisy = calibrated + noise.

SolutionGenerate one value per table position

The resulting arrays both have shape (3, 2), and every noise value passes the half-open bound checks.

Not attempted
Review

Not marked done.

Observe State Advancing Across Calls

Calling the same generator again does not restart it:

For this fixed generator and sequence of calls, both arrays satisfy the same contract and same values is False. The first request advanced the generator state, so the second request began from the next state.

The same generator can also produce bounded integers when a task needs integer positions:

Here 0 is included and 3 is excluded. The operation and dtype differ from uniform, but the useful discipline is the same: state the bounds and shape, then check them. Sampling positions receives a fuller treatment later in this chapter.

Q3. Explain two calls to one generator

Why do two consecutive calls on the same seeded generator usually produce different arrays while satisfying the same bounds?

Choose one

Select one choice, then check.

HintDistinguish seed from current state

The seed establishes a starting state. A request consumes that current state and leaves the generator at a later one.

SolutionThe request advances generator state

Both calls use the same interval and shape. The first call changes the generator's current state, so the next call produces the next values in its deterministic sequence.

Not attempted
Review

Not marked done.

Controlled variation begins with a generator and an explicit request: operation, bounds, dtype, and shape. Check those properties and the way the generated values enter the computation. A stateful generator advances after each call; the next lesson records what must match to replay that sequence.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.