Ad

Note

  • random() is not available in Swift 4.0
import Glibc // for random()

let a = random()

print(a)

This extension add a shuffled() method to any sequence.

References:

import Foundation 

extension Sequence {
  func shuffled() -> [Iterator.Element] {
    return Array(self).sorted { _,_ in drand48() < drand48() }
  }
}

// [Int] -> [Int]
print([1,2,3,4,5,6].shuffled())

// CountableClosedRange<Int> -> [Int]
print((1...6).shuffled())