shuffle an array in swift

To shuffle an array in Swift, you can use the shuffle() method of the Array class. This method randomly reorders the elements of the array.

Here is an example code snippet that shows how to shuffle an array in Swift:

main.swift
var array = [1, 2, 3, 4, 5]
array.shuffle()
print(array) // Output: [4, 1, 3, 5, 2]
84 chars
4 lines

In this code, we first create an array with some elements. Then, we call the shuffle() method of the array to shuffle its elements in a random order. Finally, we print the shuffled array to the console.

Note that the shuffle() method modifies the original array in place, so there is no need to create a new array to store the shuffled elements. If you want to create a shuffled copy of the array, you can use the shuffled() method instead.

gistlibby LogSnag