Skip to content

Instantly share code, notes, and snippets.

@brianpartridge
Forked from anonymous/superbowl_squares.swift
Last active February 2, 2018 17:32
Show Gist options
  • Save brianpartridge/4641bff6a86825dc32fe86669b9009e4 to your computer and use it in GitHub Desktop.
Save brianpartridge/4641bff6a86825dc32fe86669b9009e4 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(["Eagles", "Patriots"].map{ axis(forTeam: $0) })
@brianpartridge
Copy link
Author

I wrote the anonymous source gist, but didn't realize I wasn't logged in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment