How to Shuffle a List in Unity

by Michael Sacco Published January 14, 2024
The cover for How to Shuffle a List in Unity

Randomizing a list is the process of randomly shuffling the items in a list. When you randomize aspects of your game, you can improve the user experience by creating dynamic content for your game projects. In this article, you will learn two basic techniques in C# script in Unity for randomizing lists, along with their pros and cons.

Before diving into randomizing a list, let’s generate a list of 100 integers that we’ll work with throughout this article. To do this, we need to define a function:

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;
}

Now, use the function to generate a list of 100 integers:

List<int> integerList = GenerateIntegerList(0, 100);

Now that we have our list, let’s discuss the various methods to randomize it.

  • Generating random numbers:
    To learn how to generate random numbers in Unity, read our article on the Unity Random method.

1. Fisher-Yates Shuffle Algorithm

This algorithm, specifically the modern version, randomizes the list by starting from the last element and swapping it with an element at a randomly generated index. This process is repeated until it gets to the first element. It ensures a random permutation, giving each element an equal chance of being placed in a random position.

Using the integerList we generated earlier, here’s how the algorithm is implemented:

public List < int > ShuffleList(List < int > list)
{
    for (int i = list.Count - 1; i > 0; i--)
    {
        int j = UnityEngine.Random.Range(0, i + 1);
        int temp = list[i];
        list[i] = list[j];
        list[j] = temp;
    }
    return list;
}

This modern Fisher-Yates shuffle algorithm is efficient and easy to comprehend, making it a great choice for randomizing a list. However, it may face limitations with extremely large datasets.

2. Unity’s OrderBy Method

Another technique for randomizing a list is using Unity’s inbuilt OrderBy method. This method sorts the list based on a key selector function random.Next(), generating a new random number for each element in the list. The ToList() method is then used to convert the results into a new shuffled list.

public List<int> ShuffleListWithOrderBy(List<int> list)
{
  Random random = new Random();
  return list.OrderBy(x => random.Next()).ToList();
}

This approach is simple and convenient but less efficient than the Fisher-Yates algorithm for large lists.

Conclusion

Now, you have learned the two basic techniques of randomizing lists in Unity along with their pros and cons. Do not forget to incorporate what you have learned in your next project.

A great use for randomizing values is to help you with brainstorming. That’s how our random game generator works!

Happy coding!

Free download: Indie Game Marketing Checklist

Download now

Category

programming

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free