Random
public final class Random : PRNG
A pseudo-random number generator (PRNG) that implements
xorshift128+
, an efficient linear-feedback shift register.
Warning
Once seeded from cryptographically secure random bytes, an instance ofRandom
generates high-quality random numbers but is not a
cryptographically secure PRNG.
To generate random numbers, create your own instance of Random
with an
internal state seeded from cryptographically secure random bytes:
let random = Random()!
let x = random.uniform() as Int
// You can also pass the desired result type as an argument.
let y = random.uniform(Int.self)
if x > y {
print("Here's a random value between 0 and 42 (inclusive):")
print(random.uniform(a: 0, b: 42))
} else {
print("Here's a random value between -42 and 0 (inclusive):")
print(random.uniform(a: -42, b: 0))
}
Topics
-
The internal state of the pseudo-random number generator.
Declaration
Swift
public var state: (UInt64, UInt64)
-
Creates a pseudo-random number generator with the given internal state.
Declaration
Swift
public init(state: (UInt64, UInt64))
Parameters
state
-
The value to be used as the generator’s internal state.
-
Creates a pseudo-random number generator with an internal state seeded using cryptographically secure random bytes.
If cryptographically secure random bytes are unavailable, the result is
nil
.Declaration
Swift
public convenience init?()
-
Declaration
Swift
public func next() -> UInt64?
-
A pseudo-random number generator (PRNG) that implements
xoroshiro128+
, a successor toxorshift128+
devised by S. Vigna and D. Blackman.Warning
Once seeded from cryptographically secure random bytes, an instance ofRandom.Xoroshiro
generates high-quality random numbers but is not a cryptographically secure PRNG.To generate random numbers, create your own instance of
Random.Xoroshiro
with an internal state seeded from cryptographically secure random bytes:
See more…let random = Random.Xoroshiro()! let x = random.uniform() as Int // You can also pass the desired result type as an argument. let y = random.uniform(Int.self) if x > y { print("Here's a random value between 0 and 42 (inclusive):") print(random.uniform(a: 0, b: 42)) } else { print("Here's a random value between -42 and 0 (inclusive):") print(random.uniform(a: -42, b: 0)) }
Declaration
Swift
public final class Xoroshiro : PRNG