-
-
Save brianpartridge/4641bff6a86825dc32fe86669b9009e4 to your computer and use it in GitHub Desktop.
Superbowl Squares random number generator
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
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) }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote the anonymous source gist, but didn't realize I wasn't logged in.