Sunday, March 29, 2015

How to shuffle an array in Swift

4-2-2015 This was imported over from my roon.io blog that's now been closed down.

Had the challenge and time limit put to me the other day so here’s my working solution:
func shuffleArray(array: [Int]) -> [Int] {
    
    var tempArray = array
    for index in 0...array.count - 1 {
        let randomNumber = arc4random_uniform(UInt32(myArray.count - 1))
        let randomIndex = Int(randomNumber)
        tempArray[randomIndex] = array[index]
    }
    
    return tempArray
}

shuffleArray(myArray)
shuffleArray(myArray)
shuffleArray(myArray)

2 comments:

Feedback for my Apps.

I realized getting a hold of me directly through the App Store was difficult, so if you have an issue with any of my apps in the App Store, ...