Last active
December 11, 2018 06:53
-
-
Save natecook1000/0ac03efe07f647b46dae to your computer and use it in GitHub Desktop.
Shuffle/shuffled functions & Array extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) 2014 Nate Cook, licensed under the MIT license | |
// | |
// Fisher-Yates shuffle as top-level functions and array extension methods | |
/// Shuffle the elements of `list`. | |
func shuffle<C: MutableCollectionType where C.Index == Int>(inout list: C) { | |
let c = count(list) | |
for i in 0..<(c - 1) { | |
let j = Int(arc4random_uniform(UInt32(c - i))) + i | |
swap(&list[i], &list[j]) | |
} | |
} | |
/// Return a collection containing the shuffled elements of `list`. | |
func shuffled<C: MutableCollectionType where C.Index == Int>(var list: C) -> C { | |
shuffle(&list) | |
return list | |
} | |
extension Array { | |
/// Shuffle the elements of `self` in-place. | |
mutating func shuffle() { | |
for i in 0..<(count - 1) { | |
let j = Int(arc4random_uniform(UInt32(count - i))) + i | |
swap(&self[i], &self[j]) | |
} | |
} | |
/// Return a copy of `self` with its elements shuffled | |
func shuffled() -> [T] { | |
var list = self | |
list.shuffle() | |
return list | |
} | |
} | |
// Example of global functions | |
var strings = ["one", "two", "three", "four", "five", "six"] | |
shuffle(&strings) | |
// e.g., strings == [four, six, three, two, five, one] | |
let shuffledStrings = shuffled(strings) | |
// e.g., shuffledStrings == [six, four, one, five, three, two] | |
// Example of array methods | |
var numbers = [1, 2, 3, 4, 5, 6, 7, 8] | |
numbers.shuffle() | |
// e.g., numbers == [6, 1, 8, 3, 2, 4, 7, 5] | |
let shuffledNumbers = numbers.shuffled() | |
// e.g., mixedup == [4, 2, 6, 8, 7, 3, 5, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this repeats elements, how can I prevent this? thank you