Most algorithms you have met so far behave the same way every time on the same input. Some algorithms deliberately use random choices. This page explains the difference, when randomness helps, and where random numbers come from.
The two kinds
Randomized algorithms come in two main types:
Type
Output
Running time
Example
Las Vegas
Always correct
Random (good on average)
Randomized quicksort
Monte Carlo
Correct with high probability
Fixed bound
Miller–Rabin primality test
Python · random_quicksort.py
import randomcomparisons = 0def quicksort(a): """Randomised quicksort: the pivot is chosen at random.""" global comparisons if len(a) <= 1: return a pivot = random.choice(a) smaller, equal, larger = [], [], [] for x in a: comparisons += 1 if x < pivot: smaller.append(x) elif x > pivot: larger.append(x) else: equal.append(x) return quicksort(smaller) + equal + quicksort(larger)data = [38, 27, 43, 3, 9, 82, 10, 55]for run in range(1, 4): comparisons = 0 print(f"run {run}: {quicksort(data)} comparisons = {comparisons}")
Output (the comparison counts differ on every run)
The sorted output is identical every time; only the amount of work changes. That is a Las Vegas algorithm: random steps, predictable (correct) result.
Why use randomness?
No bad inputs. Deterministic quicksort that always picks the first element as pivot takes Θ(n2) time on already-sorted input. A random pivot makes every input equally safe: the expected time is O(nlogn) for every input.
Speed and simplicity. Monte Carlo primality tests check huge numbers far faster than known deterministic methods of similar simplicity.
Unpredictability is the goal. Card shuffling in an online game, generating passwords or encryption keys: here the output must not be predictable, and a deterministic method cannot provide that.
Where random numbers come from
A pseudorandom number generator (PRNG) is itself a deterministic algorithm: from a starting value called the seed, it produces numbers that look random. If someone learns the seed or the internal state, they can predict every future number. Fine for simulations and games of chance with no money at stake; unsafe for security.
A cryptographically secure PRNG (CSPRNG), seeded with unpredictable data, is designed so that its outputs cannot be predicted in practice. Use it for passwords, tokens and shuffling with real stakes (for example Python’s secrets module, not random).
A hardware random number generator measures physical noise (thermal noise, timing jitter). Modern operating systems mix such hardware entropy into their CSPRNG.
Key takeaways
Deterministic: same input → same steps and output, every time.
Stochastic (randomized): uses random choices; steps, time or output may vary between runs.
Las Vegas: always correct, random time; Monte Carlo: fixed time, small chance of error.
PRNGs are deterministic and predictable; security needs a CSPRNG or hardware randomness.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Differentiate deterministic and stochastic algorithms with one example of each.
Show worked answer
A deterministic algorithm performs the same steps and gives the same output every time it is run on the same input; example: Euclid’s GCD algorithm or linear search. A stochastic (randomized) algorithm uses random numbers during execution, so its steps, running time or output may differ between runs on the same input; example: randomized quicksort (always correct, running time varies — Las Vegas) or the Miller–Rabin primality test (fast, small chance of error — Monte Carlo). Stochastic algorithms are needed when the output must be unpredictable (card shuffling, key generation) and are useful to avoid bad worst-case inputs.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/basics-of-an-algorithm-and-its-properties/deterministic-and-stochastic-algorithms/ to read this page.