Array shuffle rearranges elements into a random order such that every permutation is equally likely.
You use it when unbiased randomization is required, such as sampling, randomized algorithms, or testing.
Problem
Given an array of length , produce a permutation where each of the possible orderings occurs with equal probability.
Algorithm
Use the Fisher–Yates shuffle. Iterate from the end and swap each position with a random earlier position.
shuffle(A):
n = length(A)
for i from n - 1 down to 1:
j = random integer in [0, i]
swap A[i], A[j]Example
Let
Suppose random choices are:
- , choose → swap positions and
- , choose → swap positions and
- , choose → no change
| step | i | j | array |
|---|---|---|---|
| 1 | 3 | 1 | [8, 1, 5, 3] |
| 2 | 2 | 0 | [5, 1, 8, 3] |
| 3 | 1 | 1 | [5, 1, 8, 3] |
Result:
Correctness
At step , the algorithm selects a random element from indices and places it at position . Each element has equal probability of being chosen at each step.
By induction:
- the last position is uniformly random
- the remaining positions are shuffled independently in the same way
Thus every permutation occurs with probability:
Complexity
| operation | time |
|---|---|
| shuffle |
Space usage:
When to Use
Array shuffle is appropriate when:
- unbiased random permutation is required
- randomized algorithms depend on uniform input
- sampling without replacement is needed
It is less suitable when:
- reproducibility without a fixed seed is required
- cryptographic randomness is needed, in which case a secure RNG must be used
Implementation
import random
def shuffle(a):
n = len(a)
for i in range(n - 1, 0, -1):
j = random.randint(0, i)
a[i], a[j] = a[j], a[i]import "math/rand"
func Shuffle[T any](a []T) {
n := len(a)
for i := n - 1; i > 0; i-- {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}