Concepts / Testing and Debugging Strategies

Testing and Debugging Strategies

Determinism is the property that identical inputs always produce identical outputs in a computer program—this is expected and usually desirable.

  • 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.

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.

providedproducesIdentical inputsProgramsame logicIdentical outputs
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.

initializesproducesfeeds the next stepfeeds the next stepSeedstarting valueGeneratordeterministic formulaNumber 1pseudorandom valueNumber 2next valueNumber 3next value
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

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

Comparing Two Seed Choices

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

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

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

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

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

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

determinesdeterminesSeed 42Seed 100Sequence Apseudorandom valuesSequence Bpseudorandom values
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
initializesgeneratesprovidesSeedstarting valuePhysical entropyphysical sourceAlgorithmdeterministicTruly random valuesused for securityPseudorandom valuesdesigned to appear random
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.

generatestestsif failure occursreuse recorded seedSeedrecordedVaried inputspseudorandom sequenceProgram testbehavior observedFailuresequence recordedReproduced failuresame 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

  • Assuming that computers are inherently unpredictable.

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

    Fix: Treat predictability as the normal behavior of a deterministic program.

  • Assuming that pseudorandom numbers are truly random.

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

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

  • Changing the seed when debugging a failure.

    A different seed produces a different pseudorandom sequence.

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

  • Believing that a fixed seed removes all useful variation.

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

    Fix: 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. 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.