Algorithms · Visualized

Randomized Algorithms

Randomized Algorithms

Randomness as a tool — shuffles, sampling, and probabilistic selection.

Big-O: Expected-case guarantees with simple implementations.

📖 Family Introduction

Randomized Algorithms

Use coin flips as an algorithmic resource — average-case speed and structure breaking.

Randomized algorithms use random choices to achieve guarantees that deterministic code cannot reach cheaply. Randomized quicksort and quickselect pick pivots uniformly, making adversarial inputs statistically harmless (expected O(n log n) / O(n)). Fisher–Yates shuffles every permutation with equal probability 1/n! by swapping position i with a uniformly random position in 0…i. Reservoir sampling maintains a uniform random k-sample of an unknown-length stream in one pass. Two guarantee flavours exist: Las Vegas algorithms are always correct and randomize only running time; Monte Carlo algorithms bound time but allow a small error probability.

The general recipe

  1. Identify what randomness buys: defeating adversarial order, uniform sampling, or fingerprinting.
  2. Randomize the bottleneck decision (pivot, sample, probe) with a uniform draw — check the distribution is truly uniform.
  3. Prove or test expected behaviour: for shuffles, every permutation must have probability 1/n!.
  4. Bound the tail: what is the worst case, and is it acceptable (Las Vegas) or bounded by error ε (Monte Carlo)?
  5. Seed and reproduce: use injectable RNGs so tests can replay failures.

Vocabulary you'll meet everywhere here

Uniform shuffle
every ordering equally likely — Fisher–Yates is the canonical O(n) construction
Modulo bias
rand() % k is not uniform unless k divides the RNG range — reject-and-resample fixes it
Las Vegas vs Monte Carlo
always-correct/random-time versus always-fast/probably-correct
Reservoir sampling
uniform k-sample from a stream of unknown length in O(n) time, O(k) memory
Fingerprinting
random hashing that certifies equality with high probability (Rabin–Karp, Merlin–Arthur protocols)

Where this family shows up

  • Shuffling decks, playlist randomization, A/B assignment
  • Randomized quicksort/quickselect, skip lists, Bloom filters, Monte Carlo estimation
  • Load balancing and consistent hashing in distributed systems

Common pitfalls

  • “Sort by random key” shuffles are visibly biased — use swap-based Fisher–Yates
  • Modulo bias in rand() % n for large n
  • Math.random is not cryptographically secure — tokens and secrets need CSPRNGs
  • Forgetting that Monte Carlo answers can be wrong: state and bound the error probability
Python · Randomized Algorithms in Code
import random

def shuffle(arr):
    a = arr[:]
    for i in range(len(a) - 1, 0, -1):
        j = random.randint(0, i)
        a[i], a[j] = a[j], a[i]
    return a

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.