Testing and Debugging Strategies
Testing often needs two qualities that seem to conflict: varied inputs and repeatable behavior.
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.
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.
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?
Reveal 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.
- 1Run one
The generator starts from seed 42 and follows its deterministic process to produce a sequence.
- 2Repeat seed 42
Starting the generator again with seed 42 produces the same sequence as the first run.
- 3Change the seed
Starting with seed 100 selects a different sequence, even though the underlying generator is unchanged.
- 4Apply 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.
Randomness Compared
| Pseudorandom numbers | Truly random numbers |
|---|---|
| Generated by a deterministic algorithm | Generated from a physical entropy source |
| Same seed produces the same sequence | Not described in the source as a repeatable seeded sequence |
| Useful for most applications such as games and simulations | Needed for cryptography and security |
| Designed to appear random | Provides 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.
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.
- 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 likeExpecting two runs with identical inputs to produce different results simply because they are separate runs.
Why it failsMost computer programs are deterministic and produce identical outputs for identical inputs.
Do this insteadTreat predictability as the normal behavior of a deterministic program.
Common mistake Assuming that pseudorandom numbers are truly random.
What it looks likeUsing a pseudorandom generator for a security system because its values appear random.
Why it failsPseudorandom values come from a deterministic algorithm and are not sufficient for cryptography and security.
Do this insteadUse truly random numbers from a secure physical source for security-related purposes.
Common mistake Changing the seed when debugging a failure.
What it looks likeRunning the test again with a new seed after a failure and expecting the same failure to occur.
Why it failsA different seed produces a different pseudorandom sequence.
Do this insteadReuse 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 likeConcluding that seeded testing cannot explore different cases.
Why it failsDifferent seeds produce different sequences, while a fixed seed makes one selected sequence repeatable.
Do this insteadUse different seeds for exploration and preserve a useful seed for reproduction.
Practice the Decision
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.
- 1A 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.