Skip to main content
St Louis

Back to all posts

How to Shuffle Elements Of A Mutable List In Kotlin?

Published on
3 min read
How to Shuffle Elements Of A Mutable List In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
7 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
+
ONE MORE?

In Kotlin, you can shuffle the elements of a mutable list using the shuffle() method. This method reorders the elements in a random fashion, providing a way to shuffle the list effectively.

To shuffle elements of a mutable list, follow the steps below:

  1. Start by having a mutable list with the elements you want to shuffle. For example, let's assume we have a mutable list called myList containing some integers: val myList = mutableListOf(1, 2, 3, 4, 5)
  2. Simply call the shuffle() function on your mutable list to shuffle its elements: myList.shuffle() After executing this line, the elements of myList will be rearranged randomly.
  3. If you want to shuffle the list using a custom random number generator, you can pass an instance of Random as an argument to the shuffle() method. For example: val random = Random() myList.shuffle(random) This will shuffle the elements of myList using the specified random instance.

That's it! By using the shuffle() method, you can easily shuffle the elements of a mutable list in Kotlin.

What is the easiest method to shuffle elements in a mutable list using Kotlin?

The easiest method to shuffle elements in a mutable list using Kotlin is by using the shuffle() function provided by the Kotlin standard library. This function shuffles the elements in-place within the list. Here is an example:

val list = mutableListOf(1, 2, 3, 4, 5) list.shuffle()

After calling shuffle(), the elements in the list will be shuffled randomly.

How can I rearrange the elements randomly within a mutable list in Kotlin?

To rearrange the elements randomly within a mutable list in Kotlin, you can use the shuffle function provided by the standard library. Here's an example:

fun main() { val list = mutableListOf(1, 2, 3, 4, 5) list.shuffle() println(list) }

The shuffle function randomly reorders the elements in the list. Running the code snippet above will produce different outputs each time because the elements are shuffled randomly.

Sample output:

[3, 5, 4, 2, 1]

Note that the shuffle function modifies the original list in-place. If you want to keep the original list intact, you can create a copy of the list before shuffling:

val list = mutableListOf(1, 2, 3, 4, 5) val shuffledList = list.shuffled() println(shuffledList)

The shuffled function returns a new list with the elements rearranged randomly, leaving the original list unchanged.

Sample output:

[5, 4, 2, 1, 3]

What is the most efficient way to shuffle elements in a mutable list using Kotlin?

The most efficient way to shuffle elements in a mutable list using Kotlin is by using the Collections.shuffle() function from the Java Collections framework.

Here's an example of shuffling a mutable list using Kotlin:

import java.util.Collections

fun main() { val list = mutableListOf(1, 2, 3, 4, 5)

// Shuffle the list
Collections.shuffle(list)

// Print the shuffled list
println(list)

}

This code shuffles the elements of the list in-place, modifying the original list. The Collections.shuffle() function uses an optimized version of the Fisher-Yates algorithm to ensure a uniform shuffling of the elements.