How do I shuffle an array in Swift?
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
In modern Swift, shuffling an array is built into the standard library. Most of the time you should use shuffle() to mutate an array in place or shuffled() to return a new randomized copy. The main thing to understand is when you want mutation, when you want a copy, and why some older "random sort" tricks should be avoided.
Use the Built-In APIs First
Swift already provides the two most useful operations.
Mutate the original array:
Return a shuffled copy:
These methods use the standard library’s randomization facilities and are the correct default choice in current Swift.
Know the Difference Between shuffle() and shuffled()
The names are similar, but the behavior is different:
- '
shuffle()changes the existing array' - '
shuffled()returns a new array and leaves the original untouched'
That matters in real code:
If you expected deck to change here, you chose the wrong method.
Deterministic Shuffling for Tests
In application code you normally want true randomness, but tests often need reproducible results. Swift lets you pass a custom RandomNumberGenerator.
That is useful for testing because the same seed produces the same sequence.
If You Need to Support Older Swift
Before the built-in shuffle APIs were available, developers often implemented Fisher-Yates manually. That algorithm is still worth understanding because it is the correct way to shuffle uniformly.
In modern Swift, this is mostly educational or useful only when maintaining older code.
Do Not Shuffle by Sorting with Random Comparisons
A tempting but incorrect shortcut is:
This should be avoided because:
- it does not produce a uniform shuffle
- the comparator is inconsistent
- sort algorithms expect ordering rules, not randomness
If you want a true shuffle, use shuffle(), shuffled(), or Fisher-Yates.
Performance Notes
Built-in shuffling is efficient for normal use and runs in linear time relative to the number of elements. That is the right complexity for a real shuffle because each element needs to be considered at least once.
If the array is extremely large and you only need a random sample, shuffling the whole array may do unnecessary work. In that case, consider sampling algorithms instead of a full permutation.
Common Pitfalls
The most common mistake is forgetting that shuffled() returns a new array. If you ignore the returned value, nothing changes.
Another issue is using a custom random generator incorrectly. Methods that accept a generator usually require an inout mutable generator, so remember the &generator syntax.
Developers also sometimes write their own shuffle even though the standard library already provides one. That adds maintenance burden for no gain unless you truly need custom behavior.
Finally, avoid random sorting hacks. They look short, but they are algorithmically wrong.
Summary
- Use
shuffle()to randomize an array in place. - Use
shuffled()when you want a randomized copy. - Pass a custom
RandomNumberGeneratorwhen you need deterministic test behavior. - Fisher-Yates is the correct manual algorithm for older codebases.
- Do not try to shuffle by sorting with a random comparator.
Related reading
- How do I sort a dictionary by key?
- How do I sort a dictionary by key?
- How do I sort a dictionary by value?
- How do I sort a list of objects based on an attribute of the objects?
- How do I size a UITextView to its content?
- How do I solve the INSTALL_FAILED_DEXOPT error?
- How do I sort a list of objects based on an attribute of the objects?
- How do I sort a Set to a List in Java?

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.