Imagine you are building a card game, randomizing spawn points, or creating a loot drop system. How do you ensure that the order of your items changes every time the game runs? If that’s your challenge, then you might want to learn how to shuffle a list in Unity.
Shuffling is the process of rearranging elements in a list into a random order. In Unity, this can help create dynamic gameplay experiences by ensuring that players encounter different scenarios on every run.
In this guide, you’ll learn how to implement reliable shuffling techniques in Unity to make your gameplay more dynamic and engaging.

Preparing a List To Shuffle
Before you can shuffle anything, you need a list to work with. Creating a simple list of integers is a great way to test and understand the shuffling logic before applying it to more complex objects like cards, enemies, or item data.
Here’s a clean and reusable way to generate a list of integers for testing:
public List<int> GenerateIntegerList(int min, int max)
{
List<int> list = new List<int>();
for (int i = min; i <= max; i++)
{
list.Add(i);
}
return list;
}
You can call it like this: List<int> testList = GenerateIntegerList(0, 100);
This will give you a list of integers from 0 to 100, ready to be shuffled.
Fisher-Yates Shuffle (Recommended)
The Fisher-Yates Shuffle is the most reliable way to randomize a list because it gives every element an equal chance of landing in any position. It works by iterating through the list from the end to the start and swapping each element with another element at a random index.
This method is efficient, with a time complexity of O(n), which means it scales well even for large lists. Here’s how you can implement it in Unity:
using System.Collections.Generic;
using UnityEngine;
public class ShuffleExample : MonoBehaviour
{
void Start()
{
List<int> testList = GenerateIntegerList(0, 10);
FisherYatesShuffle(testList);
foreach (int number in testList)
{
Debug.Log(number);
}
}
List<int> GenerateIntegerList(int min, int max)
{
List<int> list = new List<int>();
for (int i = min; i <= max; i++)
{
list.Add(i);
}
return list;
}
void FisherYatesShuffle(List<int> list)
{
for (int i = list.Count - 1; i > 0; i--)
{
int j = Random.Range(0, i + 1);
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
This script first generates a list of integers from 0 to 10. The FisherYatesShuffle method then randomizes that list in place. It starts from the last index and swaps that element with another randomly chosen index.
The process repeats until the first element, ensuring a uniform shuffle. When you press Play, the Debug.Log calls show the randomized order in the Console, so you can confirm the shuffle worked.
OrderBy method
The OrderBy method offers a simpler way to shuffle a list in Unity, though it’s less efficient than the Fisher-Yates algorithm, especially for large datasets. Instead of swapping elements in place, this approach assigns each element a random value and sorts the list based on those random values.
While this simplicity makes it great for quick implementations or smaller lists, it can be slower because it requires creating a new sorted list rather than modifying the original one. Here’s how you can implement it in Unity:
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ShuffleExampleOrderBy : MonoBehaviour
{
void Start()
{
List<int> testList = GenerateIntegerList(0, 10);
List<int> shuffledList = ShuffleWithOrderBy(testList);
foreach (int number in shuffledList)
{
Debug.Log(number);
}
}
List<int> GenerateIntegerList(int min, int max)
{
List<int> list = new List<int>();
for (int i = min; i <= max; i++)
{
list.Add(i);
}
return list;
}
List<int> ShuffleWithOrderBy(List<int> list)
{
System.Random random = new System.Random();
return list.OrderBy(x => random.Next()).ToList();
}
}
In this script, the ShuffleWithOrderBy method uses LINQ’s OrderBy to reorder the list. Each element gets a random key generated by random.Next(), and the list is sorted based on those random keys. Finally, ToList() converts the result into a new list, leaving your original list unchanged.
Wrapping Up
Both the Fisher-Yates algorithm and the OrderBy approach get the job done, but their use cases differ. Hence, as a best practice, always choose the algorithm based on your project’s needs.
If you’re shuffling lists frequently during gameplay, stick with Fisher-Yates to avoid unnecessary overhead. On the other hand, for one-off or low-frequency randomizations, OrderBy’s simplicity can make your scripts easier to maintain without noticeable performance trade-offs.