How to generate a random permutation in Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A random permutation is a shuffled ordering of a set of values where every element appears exactly once. In Java, the most practical way to generate one is to start with a collection and shuffle it using a proper random source. The important part is to use an algorithm that gives each ordering a fair chance rather than piecing together random swaps incorrectly.
The Simplest Approach: Collections.shuffle
For most applications, Collections.shuffle is the right answer. It applies a Fisher-Yates style shuffle to a list, which is the standard algorithm for producing an unbiased random permutation.
If you run this program multiple times, the list order changes while still containing every number from 1 to 10 exactly once.
Reproducible Shuffles
Sometimes you want the shuffle to be random but repeatable, especially in tests or simulations. In that case, pass your own Random instance with a fixed seed.
Using the same seed produces the same permutation, which is often exactly what you want in automated tests.
Building a Permutation of Indexes
A common variant is to generate a random permutation of indexes rather than shuffle the data directly. This is useful when you want to traverse another structure in random order without modifying the original data.
This is a clean pattern for sampling, randomized test ordering, and randomized processing pipelines.
Manual Fisher-Yates for Arrays
If you are working with primitive arrays and want to avoid boxing into a List<Integer>, implement the Fisher-Yates shuffle directly.
The loop goes backward, selecting a random index from the unshuffled prefix each time. That detail is what makes Fisher-Yates unbiased.
When to Use SecureRandom
If the permutation affects security-sensitive behavior such as token generation, lotteries with adversarial stakes, or security tests, use SecureRandom instead of Random.
For ordinary application logic, Random is usually fine. For security-sensitive randomness, it is not.
Common Pitfalls
A common mistake is repeatedly picking random elements and retrying duplicates until the list is full. That works, but it is less efficient and easier to get wrong than shuffling once.
Another mistake is writing a custom swap loop that uses the wrong range for the random index. If you always pick from the full array instead of the shrinking unshuffled portion, the permutation can be biased.
Developers also sometimes forget whether they need reproducibility. Tests often benefit from a fixed seed, while production behavior usually should not.
Finally, do not use Random when the shuffle affects security-sensitive outcomes. Use SecureRandom for that case.
Summary
- '
Collections.shuffleis the easiest way to generate a random permutation in Java.' - It is based on the Fisher-Yates shuffle, which is the standard unbiased approach.
- Pass a seeded
Randomwhen you need reproducible results. - Implement Fisher-Yates directly for primitive arrays if needed.
- Use
SecureRandomwhen the randomness has security implications.
Related reading
- How to generate all multiplicative partitions of a number if I have a list of primes/exponents?
- How to generate all permutations of a string in PHP?
- How to generate all the permutations of a multiset?
- How to Generate Combinations of Elements of a ListT in .NET 4.0
- How to generate a subdivided icosahedron?
- how to generate Narcissistic numbers faster?
- How to generate a secure random alphanumeric string in Java efficiently?
- How to generate buildConfigField with String type

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.