SystemRandomNumberGenerator

struct SystemRandomNumberGenerator

The system's default source of random data.

Inheritance RandomNumberGenerator

When you generate random values, shuffle a collection, or perform another operation that depends on random data, this type is the generator used by default. For example, the two method calls in this example are equivalent:

let x = Int.random(in: 1...100)
var g = SystemRandomNumberGenerator()
let y = Int.random(in: 1...100, using: &g)

SystemRandomNumberGenerator is automatically seeded, is safe to use in multiple threads, and uses a cryptographically secure algorithm whenever possible.

Platform Implementation of SystemRandomNumberGenerator

While the system generator is automatically seeded and thread-safe on every platform, the cryptographic quality of the stream of random data produced by the generator may vary. For more detail, see the documentation for the APIs used by each platform.

Initializers

init init() Required

Creates a new instance of the system's default random number generator.

Declaration

@inlinable public init()

Instance Methods

func next() -> UInt64 Required

Returns a value from a uniform, independent distribution of binary data.

Declaration

@inlinable public mutating func next() -> UInt64
func next() -> T Required

Returns a value from a uniform, independent distribution of binary data.

Use this method when you need random binary data to generate another value. If you need an integer value within a specific range, use the static random(in:using:) method on that integer type instead of this method.

Declaration

@inlinable public mutating func next<T>() -> T where T: FixedWidthInteger, T: UnsignedInteger
func next(upperBound: T) -> T Required

Returns a random value that is less than the given upper bound.

Use this method when you need random binary data to generate another value. If you need an integer value within a specific range, use the static random(in:using:) method on that integer type instead of this method.

  • Parameter upperBound: The upper bound for the randomly generated value. Must be non-zero.

Declaration

@inlinable public mutating func next<T>(upperBound: T) -> T where T: FixedWidthInteger, T: UnsignedInteger