// *** before: ***
// the C function was the usual answer, and almost every use of it had a trap
let diceOld = Int(arc4random_uniform(6)) + 1 // the +1 was forgotten often
let skewed = Int(arc4random()) % 6 // and the remainder skews the
//distribution towards small
//numbers
let coinOld = arc4random_uniform(2) == 0
let unitOld = Double(arc4random()) / Double(UInt32.max)
// a "shuffle" through a random comparison is not a shuffle at all: the
// order it gives is uneven, and the sort itself may crash on it
let shuffledOld = numbers.sorted { _, _ in arc4random_uniform(2) == 0 }
// picking an element crashed on an empty array
let anyOld = numbers[Int(arc4random_uniform(UInt32(numbers.count)))]
// and arc4random lives in Darwin: on Linux the same file did not compile
// *** in version 4.2: ***
let dice = Int.random(in: 1...6) // the bounds are written as they are read
let coin = Bool.random()
let unit = Double.random(in: 0..<1)
let letter = "abcdef".randomElement()
var deck = numbers
deck.shuffle() // in place
let shuffled = numbers.shuffled() // or a new array
let any = numbers.randomElement() // an Optional: nil on an empty array
// the distribution is even, the empty case is honest, and the source may be
// given explicitly - a repeatable one makes a test repeatable
var generator = SystemRandomNumberGenerator()
let again = Int.random(in: 1...6, using: &generator)
let pick = numbers.randomElement(using: &generator)