Math Fundamentals18 sections · 814 units
Open in Course

Problem - Shuffle an Array

Fair randomization

You need to shuffle an array so that every permutation is equally likely. This is harder than it sounds.

Given an array of integers, implement a class that can reset the array to its original state and shuffle it randomly. Each permutation must have equal probability of 1n!\frac{1}{n!} where nn is the array length.

Before reading on, think about this: if you pick random swaps without a plan, will every permutation have equal chance?