Skip to content
DeveloperMemos

Shuffling Lists Randomly in Kotlin

Kotlin, Lists, Shuffle2 min read

In this article, we will learn how to shuffle a list of elements randomly in Kotlin.

In order to shuffle a list in Kotlin, we can use the shuffled() function. This function is a extension function that can be called on any List object and it returns a new List with the elements in a random order.

Using shuffled()

Here is an example of how to use the shuffled() function:

1// Create a list of elements
2val numbers = listOf(1, 2, 3, 4, 5)
3
4// Shuffle the list randomly
5val shuffledNumbers = numbers.shuffled()
6
7// Print the shuffled list
8println(shuffledNumbers) // Output: [3, 5, 1, 4, 2] (the order of the elements will be different each time)

As we can see in the example, we first create a list of elements and then we call the shuffled() function on it to get a new list with the elements in a random order. The shuffled() function does not modify the original list, it returns a new list with the shuffled elements.

Using shuffle()

In addition to the shuffled() function, Kotlin also provides the shuffle() function, which is similar to shuffled() but it shuffles the elements in the original list instead of returning a new list. Here is an example of how to use the shuffle() function:

1// Create a list of elements
2val numbers = mutableListOf(1, 2, 3, 4, 5)
3
4// Shuffle the list randomly
5numbers.shuffle()
6
7// Print the shuffled list
8println(numbers) // Output: [3, 2, 5, 1, 4] (the order of the elements will be different each time)

As we can see in this example, we first create a mutableListOf and then we call the shuffle() function on it. The shuffle() function modifies the original list, so we don't need to assign the result to a new variable. After calling the shuffle() function, the elements in the original list will be in a random order.

Providing a generator yourself

Both the shuffle() and shuffled() functions use a default random number generator to shuffle the elements in the list. However, we can also provide our own random number generator to these functions if we want to use a specific random number generator. Here is an example of how to do this:

1// Create a list of elements
2val numbers = listOf(1, 2, 3, 4, 5)
3
4// Create a random number generator
5val random = Random()
6
7// Shuffle the list using the provided random number generator
8val shuffledNumbers = numbers.shuffled(random)
9
10// Print the shuffled list
11println(shuffledNumbers) // Output: [2, 5, 3, 4, 1] (the order of the elements will be different each time)

In this example, we first create a list of elements and then we create a Random object that we will use as the random number generator. We then call the shuffled() function and pass it the Random object as an argument. This will cause the shuffled() function to use the provided random number generator to shuffle the elements in the list.

In Conclusion

Shuffling a list in Kotlin is a simple task that can be done using the shuffle() or shuffled() functions. These functions allow us to shuffle a list of elements randomly, either by modifying the original list or by returning a new list with the shuffled elements. In addition, we can also provide our own random number generator to these functions if we want to use a specific random number generator.

Shuffling a list can be useful in many different scenarios, such as when we want to randomize the order of elements in a list or when we want to create a random sample from a larger list of elements. With the shuffle() and shuffled() functions, we can easily implement these and other similar operations in our Kotlin code.