Chapter 13

Controlled and Repeatable Experiments

Add bounded variation to a numerical measurement, replay one sequence, preserve paired records while sampling and splitting, and save enough evidence to compare two runs.

The previous chapter plotted fixed measurements. Real measurements often vary, so the next step is to add variation without losing control of the computation. We begin with one NumPy generator and an explicit range and shape.

A seed starts a sequence, but replay also depends on the generator state and the order of its calls. The same discipline applies when we sample records or split them into groups: one set of indexes must move every aligned value together.

The final lessons save the settings, observation identifiers, measurements, and output path from one run. We then reconstruct the run, compare discrete and floating-point results appropriately, and distinguish a reproduction from a comparison that changes one named setting. This completes the Python Core path with an experiment ready for later Mathematics chapters and projects.

After this chapter

  • Generate bounded numerical variation with an explicit range and shape.
  • Replay one sequence by preserving its seed, generator ownership, and call order.
  • Sample records through indexes with an explicit replacement rule.
  • Shuffle and split aligned records with one shared permutation, then check disjointness and coverage.
  • Save a JSON-compatible run record containing its configuration, observation identifiers, measurements, summary, and output path.
  • Compare a rerun with exact checks for discrete values and tolerance-aware checks for floating-point results, then change one named setting for a controlled comparison.

Lessons

  1. 01
    Generate Controlled Variation

    Create a NumPy generator, add bounded uniform noise to a small table, and verify its requested shape and half-open interval.

    3 exercises
  2. 02
    Replay One Random Sequence

    Trace generator state, replay a complete noisy run, pass a generator into a function, and avoid accidental reseeding.

    3 exercises
  3. 03
    Sample with an Explicit Rule

    Sample record indexes with or without replacement, inspect their shape, and preserve identity across aligned arrays.

    3 exercises
  4. 04
    Shuffle and Split Paired Records

    Create working and checking groups from one shared permutation without silently breaking paired records.

    3 exercises
  5. 05
    Record and Save One Run

    Reuse dataclass records to save a seed, sample count, observation identifiers, summary, and figure path as inspectable run evidence.

    3 exercises
  6. 06
    Rerun and Compare

    Check configuration before output, replay generated and selected values, and change exactly one recorded setting for a controlled comparison.

    3 exercises

Review and practice

  1. Review

    Review variation contracts, seed and call order, selected indexes, shared permutations, JSON records, replay, and controlled comparison.

  2. Exercises

    Apply the complete Chapter 13 path through ten cumulative contracts, implementations, checks, and diagnoses.

Chapter progress