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