Determinism
Table of Contents
A function or algorithm is deterministic if, for any given input, it always produces exactly the same output — no matter when it runs, on what machine, or how many times it is called. The output depends solely on the input, not on external state.
Related: Functional Programming Immutability Iterators
Pure Functions
Determinism is one of two requirements for a pure function:
- Deterministic — same input always yields same output
- No side effects — does not read from or write to external state
# Pure — deterministic, no side effects def add(a, b): return a + b def square(x): return x ** 2 # Impure — non-deterministic (depends on external state) import random, time def random_value(): return random.randint(0, 100) # different each call def current_timestamp(): return time.time() # different each call # Impure — has side effects (modifies external state) total = 0 def accumulate(x): global total total += x # modifies global variable return total
Non-Deterministic Sources
Functions become non-deterministic when they depend on:
| Source | Example |
|---|---|
| Random values | random.random(), os.urandom() |
| Current time | datetime.now(), time.time() |
| External I/O | Reading a file, database query, HTTP request |
| Global or shared state | Global variables, class attributes |
| Thread scheduling | Race conditions in concurrent code |
| Hardware | Floating-point rounding on different CPUs |
Benefits
Testability
Pure functions are trivially testable — no mocking of databases, no freezing time, no managing shared state between tests:
def test_add(): assert add(2, 3) == 5 # always passes, always reproducible
Reproducibility
A bug in a deterministic function is 100% reproducible with the same inputs — no "works on my machine" timing issues or hidden state to reconstruct.
Safe concurrency
Deterministic functions can run in parallel without locks — they don't read from or write to shared state, so there are no race conditions:
from concurrent.futures import ProcessPoolExecutor # Safe to parallelise — pure function with ProcessPoolExecutor() as executor: results = list(executor.map(square, range(1000)))
Memoisation
Because the output depends only on the input, results can be safely cached — the same input will always produce the same output:
from functools import lru_cache @lru_cache(maxsize=None) def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2)
Practical Boundaries
Fully pure programs cannot exist — at some point software must read input, write output, query a database, or get the current time. The practical goal is to:
- Push non-determinism to the edges (I/O boundary)
- Keep the core logic as pure functions
- Thread external values (time, random seed, config) in as parameters rather than calling them from inside functions
# Instead of: def create_session(): return {"created_at": time.time()} # non-deterministic # Prefer: def create_session(timestamp): # deterministic return {"created_at": timestamp} # Call site handles the non-deterministic part: session = create_session(time.time())
Tips
- Functions that are deterministic and have no side effects are the easiest to test, reason about, and refactor — default to writing them this way unless there is a specific reason not to
- Threading external state in as parameters rather than reading it inside a function makes the dependency explicit and the function testable
- In security: deterministic hash functions are essential —
sha256(data)must always return the same value for the same input, which is why cryptographic hashes are designed to be deterministic (unlike random functions)