Concepts / Testing and Debugging Strategies

Testing and Debugging Strategies

Testing often needs two qualities that seem to conflict: varied inputs and repeatable behavior.

  • Programming

When Predictability Helps

Many people initially expect a computer to behave unpredictably: run the same program twice and perhaps receive different results. Most computer programs behave differently from that expectation. They are deterministic, meaning that identical inputs produce identical outputs. This is usually desirable because predictable behavior makes programs easier to understand, test, and debug.

In the real world

Adding 2 and 3 produces 5 each time, and sorting the same list produces the same order each time. These are simple examples of determinism: the operation and its inputs stay the same, so the result stays the same.

Identical inputsProgramsame logicIdentical outputsprovidedproduces
What happens when identical inputs are given to a deterministic program, and why are the outputs always identical?

Tracing a Pseudorandom Sequence

Games and simulations often need variation. If a game always behaved exactly the same way, playing it again would not be fun. If a simulation always produced one identical result, it could not explore different possible scenarios. Pseudorandom numbers address this need without abandoning deterministic computation.

Seedstarting valueGeneratordeterministic formulaNumber 1pseudorandom valueNumber 2next valueNumber 3next valueinitializesproducesfeeds the next stepfeeds the next step
How does using the same seed cause a pseudorandom number generator to produce the same sequence every time?

A pseudorandom number generator starts with a seed, applies a deterministic mathematical formula, and produces a pseudorandom number. That result then becomes the input for the next application of the formula. Repeating this process creates a sequence. The sequence is deterministic internally, but its values are designed to appear random.

What do you think happens?

A pseudorandom generator is started twice with the same seed. What should you expect from the two generated sequences?

  • The sequences will be the same

  • The sequences must be different

  • The first values will match, but later values cannot match

Reveal answer
Answer:

The sequences will be the same.

The seed is the generator's starting value, and the generation process is deterministic. Therefore, the same seed produces the same sequence.

Seeds and Reproducibility

Definition

A seed is the starting value for a pseudorandom number generator. It determines which sequence of pseudorandom numbers the generator will produce.

Worked example

Comparing Two Seed Choices

Consider two runs of the same pseudorandom generator: one initialized with seed 42 and another initialized with seed 100.

  1. 1
    Run one

    The generator starts from seed 42 and follows its deterministic process to produce a sequence.

  2. 2
    Repeat seed 42

    Starting the generator again with seed 42 produces the same sequence as the first run.

  3. 3
    Change the seed

    Starting with seed 100 selects a different sequence, even though the underlying generator is unchanged.

  4. 4
    Apply the choice

    A fixed seed supports consistent testing and debugging, while a changing seed can make repeated user experiences less repetitive.

Result

The seed controls reproducibility: the same seed repeats a sequence, while a different seed produces a different sequence.

Seed 42Seed 100Sequence Apseudorandom valuesSequence Bpseudorandom valuesdeterminesdetermines
How does changing the seed change the starting point and resulting sequence of pseudorandom numbers?

Randomness Compared

Pseudorandom numbersTruly random numbers
Generated by a deterministic algorithmGenerated from a physical entropy source
Same seed produces the same sequenceNot described in the source as a repeatable seeded sequence
Useful for most applications such as games and simulationsNeeded for cryptography and security
Designed to appear randomProvides randomness from a physical source

The important distinction is not whether pseudorandom values look random, but how they are produced and whether predictability is acceptable for the task.

Seedstarting valuePhysical entropyphysical sourceAlgorithmdeterministicTruly random valuesused for securityPseudorandom valuesdesigned to appear randominitializesgeneratesprovides
What is the difference between numbers produced by an unpredictable physical source and numbers generated by a deterministic algorithm?

Testing with Controlled Variation

Testing often needs two qualities that seem to conflict: varied inputs and repeatable behavior. Pseudorandom generation provides both. Different seeds can expose a program to different sequences of inputs. When a particular sequence reveals a failure, keeping the associated seed allows the same sequence to be generated again during debugging.

SeedrecordedVaried inputspseudorandom sequenceProgram testbehavior observedFailuresequence recordedReproduced failuresame seedgeneratestestsif failure occursreuse recorded seed
How can a program use varied pseudorandom inputs while still allowing a failure to be reproduced during debugging?
  • Use a fixed seed when you need a test or debugging session to behave consistently.
  • Try different seeds when you want to explore different possible scenarios.
  • When a failure appears, preserve the seed so the generated sequence can be reproduced.
  • Use a changing seed, such as the current time in milliseconds, when a user-facing program should produce a different sequence on each run.
  • Use truly random values from a secure physical source when the purpose is cryptography or security.

Mistakes About Randomness

  • Common mistake

    Assuming that computers are inherently unpredictable.

    What it looks like

    Expecting two runs with identical inputs to produce different results simply because they are separate runs.

    Why it fails

    Most computer programs are deterministic and produce identical outputs for identical inputs.

    Do this instead

    Treat predictability as the normal behavior of a deterministic program.

  • Common mistake

    Assuming that pseudorandom numbers are truly random.

    What it looks like

    Using a pseudorandom generator for a security system because its values appear random.

    Why it fails

    Pseudorandom values come from a deterministic algorithm and are not sufficient for cryptography and security.

    Do this instead

    Use truly random numbers from a secure physical source for security-related purposes.

  • Common mistake

    Changing the seed when debugging a failure.

    What it looks like

    Running the test again with a new seed after a failure and expecting the same failure to occur.

    Why it fails

    A different seed produces a different pseudorandom sequence.

    Do this instead

    Reuse the seed associated with the failure to reproduce the same sequence.

  • Common mistake

    Believing that a fixed seed removes all useful variation.

    What it looks like

    Concluding that seeded testing cannot explore different cases.

    Why it fails

    Different seeds produce different sequences, while a fixed seed makes one selected sequence repeatable.

    Do this instead

    Use different seeds for exploration and preserve a useful seed for reproduction.

Practice the Decision

MEDIUM

A simulation should explore different scenarios during development, but a developer must be able to investigate a scenario again after a failure. Explain how pseudorandom numbers and seeds can support both goals. Then state whether pseudorandom numbers are appropriate for a security system and why.

Hints
  • Separate the purpose of exploring scenarios from the purpose of reproducing one scenario.
  • Recall what happens when the same seed is used again.
  • Compare deterministic algorithms with physical entropy sources.
  1. 1
    A strong answer explains that different seeds create different pseudorandom sequences for exploration, while recording and reusing a seed makes a particular sequence reproducible for debugging. It also explains that pseudorandom numbers are not sufficient for cryptography or security, which require truly random numbers from a secure physical source.

Key Takeaways

  • Determinism means that identical inputs produce identical outputs, and this predictability makes testing and debugging possible.
  • Pseudorandom numbers come from deterministic algorithms but are designed to appear random.
  • A seed determines the pseudorandom sequence: the same seed reproduces the same sequence, while a different seed produces a different sequence.
  • Fixed seeds support reproducible tests and debugging; changing seeds support variety in games and simulations.
  • Security and cryptographic applications require truly random numbers from secure physical entropy sources rather than ordinary pseudorandom numbers.

Related concepts

More concepts starting with T