Functions and Their Inputs and Outputs
Determinism is the property that identical inputs always produce identical outputs in a computer program—this is expected and usually desirable.
From Input to Output
A useful way to think about a computer program is to follow information through it. An input enters a function or program, the program applies its instructions, and an output is produced. When the same input produces the same output each time, the behavior is deterministic. For example, adding 2 and 3 produces 5 every time, and sorting the same list produces the same order. This predictability is usually desirable because it makes programs easier to understand, test, and debug.
The diagram shows the basic relationship behind a deterministic computation. The function receives an input, applies its instructions, and produces an output. If the relevant input is identical on two runs and the program is deterministic, the resulting output is identical as well.
Deterministic Behavior
Determinism is the property that identical inputs always produce identical outputs in a computer program.
Tracing a Repeated Calculation
Suppose a deterministic calculation receives the same inputs, 2 and 3, on two separate runs.
First input: The calculation receives 2 and 3.
First output: The calculation produces 5.
Second input: The same values, 2 and 3, are supplied again.
Second output: Because the inputs are identical and the behavior is deterministic, the output is 5 again.
Both runs produce the same output: 5.
The Need for Unpredictability
Determinism is useful, but some applications need behavior that appears unpredictable. A game that behaved exactly the same way every time would be less interesting to play. A simulation of weather or disease spread would be less useful if it could explore only one identical result. Security systems also need unpredictability because an attacker who can predict program behavior may be able to exploit it.
This creates a design challenge: the program itself remains deterministic, yet its visible behavior should vary. Pseudorandom number generation addresses this challenge. It uses a deterministic algorithm to produce numbers that are designed to appear random.
| Property | Pseudorandom numbers | Truly random numbers |
|---|---|---|
| How they are produced | A deterministic algorithm | A physical entropy source |
| Repeatability | The same starting seed produces the same sequence | The source is used for randomness rather than a repeatable algorithmic sequence |
| Typical use | Games, simulations, testing, and debugging | Cryptography and security |
| Main strength | Useful variety with reproducibility when needed | Unpredictability from a physical source |
Following a Seeded Sequence
A seed is the starting value for a pseudorandom number generator. The seed determines which sequence of pseudorandom numbers the generator produces.
A pseudorandom generator begins with a seed. It applies a deterministic mathematical formula to that starting value and produces a pseudorandom number. That generated number then becomes the input to the formula again, producing the next number. The process can continue for as many values as the program needs.
What do you think happens?
If a program starts its pseudorandom generator with seed 42 twice, what should happen to the generated sequences?
Reveal answer
Answer: The sequences will be identical.
The seed is the starting value, and the generator uses a deterministic algorithm. Reusing the same seed recreates the same sequence.
Reproducing Separate Runs
Making a Simulation Reproducible
A developer is testing a simulation that uses pseudorandom numbers. The developer wants to run the same scenario again after finding a possible bug.
Choose a fixed seed: The developer initializes the pseudorandom generator with a fixed starting value, such as 42.
Run the simulation: The generator produces its deterministic sequence, and the simulation uses those values.
Repeat the run: The developer initializes the generator with the same seed and runs the simulation again.
Compare behavior: The same sequence is recreated, so the developer can investigate the same behavior again.
A fixed seed makes the simulation's pseudorandom sequence reproducible, which supports testing and debugging.
Choosing the Right Randomness
The right choice depends on the purpose of the program. Pseudorandom numbers are sufficient for many applications, including games and simulations. Their deterministic foundation is useful when a developer needs variety during normal use and reproducibility during testing. A fixed seed supports consistent behavior, while a seed that changes each time can produce a different sequence on each run.
Assuming that computers are inherently unpredictable.
Most computer programs are deterministic and produce identical outputs from identical inputs.
Fix:
Treat repeatability as the normal behavior unless the program deliberately uses a source of variation.Thinking that pseudorandom means truly random.
Pseudorandom numbers come from deterministic algorithms, while security requires truly random numbers from a physical entropy source.
Fix:
Use pseudorandom numbers for suitable applications such as games and simulations, and use a truly random source for cryptography and security.Changing the seed when trying to reproduce a bug.
The seed determines the generated sequence, so a different seed produces a different sequence.
Fix:
Record and reuse a fixed seed when reproducibility is the goal.Believing that a fixed seed removes all useful variation permanently.
A fixed seed is useful for development and testing, while a changing seed can provide variety for users.
Fix:
Choose a fixed seed for reproducible tests and a changing seed when varied runs are desired.
Apply the Decision
For each situation, decide whether the main priority is deterministic behavior, pseudorandom behavior with reproducibility, pseudorandom behavior with variety, or truly random behavior from a physical source: a calculation that must be tested repeatedly; a game that should feel different on different runs; a simulation whose unusual result must be investigated again; and a security system that must resist prediction.
Hints
- Testing and debugging benefit from repeatable behavior.
- A fixed seed reproduces a pseudorandom sequence.
- A changing seed can produce a different sequence on each run.
- Security needs truly random numbers from a physical entropy source.
Checking the Choices
Match each situation with the behavior it needs.
Repeated calculation: Use deterministic behavior because identical inputs should produce identical outputs.
Game with varied runs: Use pseudorandom behavior with a changing seed so the program can appear different on different runs.
Simulation under investigation: Use pseudorandom behavior with a fixed seed so the same sequence and behavior can be recreated.
Security system: Use truly random numbers from a physical entropy source because pseudorandom numbers alone are not sufficient for security.
The correct choice depends on whether the priority is repeatability, variety, or resistance to prediction.
Key Takeaways
- A function or program transforms inputs into outputs; deterministic behavior means identical inputs produce identical outputs.
- Determinism is valuable for reliable testing, debugging, and repeatable calculations.
- Pseudorandom numbers come from deterministic algorithms but are designed to appear random.
- A seed is the starting value that determines a pseudorandom sequence; reusing the same seed reproduces the same sequence.
- Pseudorandom numbers suit many games and simulations, but cryptography and security need truly random numbers from physical entropy sources.
Key Takeaways
- Determinism connects identical inputs with identical outputs.
- Pseudorandom generation uses deterministic steps to create sequences that appear random.
- The seed selects the sequence, so the same seed enables reproducibility.
- Fixed seeds help testing and debugging, while changing seeds provide variety between runs.
- Truly random physical sources are required for cryptography and security rather than pseudorandomness alone.