protocol RandomNumberGenerator
Inheritance | View Protocol Hierarchy → |
---|---|
Import | import Swift |
Instance Methods
Returns a value from a uniform, independent distribution of binary data.
Returns: An unsigned 64-bit random value.
Declaration
mutating func next() -> UInt64
Default Implementations
Returns a value from a uniform, independent distribution of binary data.
Returns: A random value of T
. Bits are randomly distributed so that
every value of T
is equally likely to be returned.
Declaration
mutating func next<T>() -> T where T : FixedWidthInteger, T : UnsignedInteger
Returns a random value that is less than the given upper bound.
upperBound
: The upper bound for the randomly generated value.
Returns: A random value of T
in the range 0..<upperBound
. Every
value in the range 0..<upperBound
is equally likely to be returned.
Declaration
mutating func next<T>(upperBound: T) -> T where T : FixedWidthInteger, T : UnsignedInteger
A type that provides uniformly distributed random data.
When you call methods that use random data, such as creating new random values or shuffling a collection, you can pass a
RandomNumberGenerator
type to be used as the source for randomness. When you don't pass a generator, the defaultRandom
type is used.When providing new APIs that use randomness, provide a version that accepts a generator conforming to the
RandomNumberGenerator
protocol as well as a version that uses the default generator. For example, thisWeekday
enumeration provides static methods that return a random day of the week:Conforming to the RandomNumberGenerator Protocol
A custom
RandomNumberGenerator
type can have different characteristics than the defaultRandom
type. For example, a seedable generator can be used to generate the same sequence of random values for testing purposes.To make a custom type conform to the
RandomNumberGenerator
protocol, implement the requirednext()
method. Each call tonext()
must produce a uniform and independent random value.Types that conform to
RandomNumberGenerator
should specifically document the thread safety and quality of the generator.