dram.me

Swift获取随机数

补遗

  1. 可以直接利用Foundation中的FileHandle读取文件,不需要用Glibc。—— 2017-05-18

数值类型安全中一文中有将Swift获取随机数作为示例呈现,以下代码基于/dev/urandom实现:

import func Glibc.close
import func Glibc.open
import func Glibc.read
import var Glibc.O_RDONLY

let count = 5

let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: count)

let fd = open("/dev/urandom", O_RDONLY)
read(fd, buffer, count)
close(fd)

for i in 0..<count {
    print(buffer[i])
}

buffer.deallocate(capacity: count)