Skip to content

Instantly share code, notes, and snippets.

@markawil
Created March 19, 2015 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markawil/b1b9d3da47e9a5c515b0 to your computer and use it in GitHub Desktop.
Save markawil/b1b9d3da47e9a5c515b0 to your computer and use it in GitHub Desktop.
Swift shuffle array
var myArray = [1,2,3,4,5,6,7,8]
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment