获取随机数

25 Aug 2017 by 晓晨


arc4random


import Darwin.C.stdlib
// included in UIKit

//public func arc4random() -> UInt32
//public func arc4random_uniform(_ __upper_bound: UInt32) -> UInt32

// Where does the "arc4" come from?
// The arc4random() function uses the key stream generator employed by
// the arc4 cipher, which uses 8*8 8 bit S-Boxes.
// -- https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/arc4random.3.html

arc4random() // e.g. 1483293806
arc4random_uniform(100) // 0 - 99
arc4random_uniform(100) + 1 // 1 - 100

因为返回值是 UInt32 类型,通常会先转换成 Int。