Random Permutation Generator
Result
3,1,4,5,2
How it works
A random permutation is an arrangement of a set of items in a randomly selected order, where each of the n! possible orderings is equally likely. Random permutations are used in experimental design, Monte Carlo methods, shuffling algorithms, and statistical testing.
**Fisher-Yates shuffle algorithm** The standard algorithm for generating a uniform random permutation in O(n) time: for i from n−1 down to 1, swap element i with a randomly chosen element from 0 to i (inclusive). This produces every permutation with equal probability 1/n!. The naive approach (sort by random key) is also correct but O(n log n). Incorrect approaches (swapping with any position rather than 0..i) produce non-uniform distributions — a common bug in naive shuffle implementations.
**Cryptographically secure vs. pseudo-random** For statistical applications and games, a pseudo-random number generator (PRNG like Mersenne Twister) is sufficient. For security-sensitive uses (randomly assigning subjects in a clinical trial, shuffling a card deck for real money gambling, randomizing treatment assignments), use a cryptographically secure PRNG (CSPRNG) seeded from hardware entropy. This tool uses the browser's crypto.getRandomValues() — a CSPRNG.
**Applications** k-fold cross-validation: randomly permute dataset indices before splitting into k folds. Latin square experimental design. Bootstrapping: random sampling with replacement (permutation is sampling without replacement). Randomized controlled trials: random treatment assignment.
Frequently Asked Questions
- In Fisher-Yates: for position i from n−1 down to 1, swap element i with a random element from positions 0..i (inclusive). This ensures each position is equally likely to receive each element. The naive approach (swap with any random position 0..n−1) produces a biased distribution — some permutations are more likely than others. With n=3 items and naive shuffle: 3^3=27 outcomes, but there are only 3!=6 permutations — 27 is not divisible by 6, so some permutations are assigned to more outcomes than others.
- Standard PRNGs (Mersenne Twister, PCG, xorshift) are NOT cryptographically secure — a 624-output Mersenne Twister can be fully reconstructed from 624 consecutive outputs, allowing an attacker to predict future random values. For security-sensitive shuffles (card games with real money, randomized audit selection, clinical trial randomization), use a CSPRNG seeded from hardware entropy. This tool uses crypto.getRandomValues() — the browser's CSPRNG, which is cryptographically secure.
- n! (n factorial): 5 items → 120 permutations. 10 items → 3,628,800. 20 items → 2.43 × 10^18. 52 cards in a shuffled deck → 52! ≈ 8 × 10^67. A PRNG with a 32-bit seed can only generate 2^32 ≈ 4 billion distinct states — far fewer than the number of valid deck shuffles. For truly random permutations of large sets, you need a CSPRNG with sufficient state (the Web Crypto API uses 256+ bits of entropy).
- A complete shuffle generates a random permutation of all n items. Sampling k items without replacement from n items generates the first k elements of a random permutation. Fisher-Yates handles both: to sample k from n, run only the first k iterations of the algorithm and take the last k elements swapped out. This is equivalent to a full shuffle but stops early — O(k) rather than O(n), important when k << n.