Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2017 19:06
Show Gist options
  • Save anonymous/bee817387535651f08b081b5cf72e9f8 to your computer and use it in GitHub Desktop.
Save anonymous/bee817387535651f08b081b5cf72e9f8 to your computer and use it in GitHub Desktop.
Superbowl Squares random number generator
import Foundation
/// http://stackoverflow.com/a/24029847/4992155
extension MutableCollection where Indices.Iterator.Element == Index {
/// Shuffles the contents of this collection.
mutating func shuffle() {
let c = count
guard c > 1 else { return }
for (unshuffledCount, firstUnshuffled) in zip(stride(from: c, to: 1, by: -1), indices) {
let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
swap(&self[firstUnshuffled], &self[i])
}
}
}
extension Sequence {
/// Returns an array with the contents of this sequence, shuffled.
func shuffled() -> [Iterator.Element] {
var result = Array(self)
result.shuffle()
return result
}
}
func axis(forTeam team: String) -> (String, [Int]) {
return (team, (0...9).shuffled())
}
print(["Falcons", "Patriots"].map{ axis(forTeam: $0) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment