How can Kotlin shuffle the input list and output it?

In Kotlin, you can use the shuffle() function to randomly shuffle the elements of a list. Here is an example code:

fun main() {
    val list = listOf(1, 2, 3, 4, 5)
    val shuffledList = list.shuffled()
    println(shuffledList)
}

In the above example code, we first create a list containing integers. Then, we use the shuffled() function to randomly shuffle the elements in the list and assign the result to shuffledList. Finally, we use the println() function to output the shuffled list to the console.

When running the above code, the output will be a list in random order, for example [4, 2, 5, 1, 3].

bannerAds