结构
System
SystemRandomNumberGenerator
系统的默认随机数据源。
声明
@frozen struct SystemRandomNumberGenerator
概述
当您生成随机值、打乱集合或执行其他依赖于随机数据的操作时,此类型是默认使用的生成器。例如,本例中的两个方法调用是等价的:
let x = Int.random(in: 1...100)
var g = SystemRandomNumberGenerator()
let y = Int.random(in: 1...100, using: &g)
SystemRandomNumberGenerator
是自动播种的,可以在多个线程中安全使用,并尽可能使用加密安全算法。
`SystemRandomNumberGenerator` 的平台实现
虽然系统生成器在每个平台上都是自动播种和线程安全的,但生成器生成的随机数据流的加密质量可能会有所不同。有关更多详细信息,请参阅每个平台使用的 API 的文档。
-
Apple 平台使用
arc4random_buf(3)
。 -
Linux 平台在可用时使用
getrandom(2)
;否则,他们从/dev/urandom
读取。 -
Windows 使用
BCryptGenRandom
。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift String.UTF8View first用法及代码示例
- Swift SetAlgebra isStrictSubset(of:)用法及代码示例
- Swift Slice endIndex用法及代码示例
- Swift Substring.UnicodeScalarView insert(contentsOf:at:)用法及代码示例
- Swift String.UTF16View firstIndex(where:)用法及代码示例
- Swift String init()用法及代码示例
- Swift String.UTF16View last(where:)用法及代码示例
- Swift String.UnicodeScalarView flatMap(_:)用法及代码示例
- Swift String.UTF8View split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代码示例
- Swift Substring.UTF16View enumerated()用法及代码示例
- Swift String init(_:radix:uppercase:)用法及代码示例
- Swift String.UTF8View elementsEqual(_:)用法及代码示例
- Swift String.UnicodeScalarView min(by:)用法及代码示例
- Swift Sequence prefix(_:)用法及代码示例
- Swift Set subtracting(_:)用法及代码示例
- Swift SIMD4 .!=(_:_:)用法及代码示例
- Swift Slice reduce(into:_:)用法及代码示例
- Swift Sequence prefix(while:)用法及代码示例
- Swift Slice contains(_:)用法及代码示例
- Swift Substring.UTF16View sorted(by:)用法及代码示例
- Swift StrideThrough map(_:)用法及代码示例
- Swift String.UnicodeScalarView lastIndex(of:)用法及代码示例
- Swift Slice removeAll(where:)用法及代码示例
- Swift Set remove(_:)用法及代码示例
- Swift Set prefix(upTo:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 SystemRandomNumberGenerator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。