import Cocoa
/* Create a array of months from Jan to Jun */
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
print(months)
/* Create a function to swap random elements in the array */
func swapRandomElements(array: inout [String]) {
let firstIndex = Int(arc4random_uniform(UInt32(array.count)))
let secondIndex = Int(arc4random_uniform(UInt32(array.count)))
let temp = array[firstIndex]
array[firstIndex] = array[secondIndex]
array[secondIndex] = temp
}
/* swap the months */
swapRandomElements(array: &months)
print(months)
/* swap the months 2 times */
swapRandomElements(array: &months)
swapRandomElements(array: &months)
print(months)
/* swap the months 10 times */
for _ in 1...10 {
swapRandomElements(array: &months)
}
print(months)
Output
["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
["Jan", "Apr", "Mar", "Feb", "May", "Jun"]
["Jan", "Mar", "Jun", "Feb", "May", "Apr"]
["May", "Apr", "Jun", "Feb", "Mar", "Jan"]