How to shuffle an array in Swift

4-2-2015 This was imported over from my 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



Post a Comment

Popular posts from this blog

Gradient UINavigationBar in Swift

Simple Xamarin iOS Keychain code

Simple C# Dependency Resolver, or the beginning of your own IoC container