Protocols
The following protocols are available globally.
-
A pseudo-random number generator (PRNG).
Reference types that conform to
PRNG
are infinite sequences of pseudo-random elements. Protocol extension methods iterate over such a sequence as necessary to generate pseudo-random values from the desired distribution.Considerations for Conforming Types
For clarity to end users, custom PRNGs may be implemented in an extension to
Random
. For instance, thexoroshiro128+
algorithm is implemented in a final class namedRandom.Xoroshiro
.The static methods
_entropy(_:)
and_entropy(_:count:)
return cryptographically secure random bytes that may be useful for seeding your custom PRNG. However, these methods may returnnil
if the requested number of random bytes is not available, and they are not recommended as a routine source of random data.Adding Other Probability Distributions
Many built-in protocol extension methods make use of the primitive, overloaded method
_random(_:bitCount:)
. You may wish to use the same method in new protocol extension methods that return pseudo-random values from other probability distributions.The method
_random(_:bitCount:)
generates uniformly distributed binary floating-point values in the half-open range [0, 1) with a precision of eitherbitCount
or the significand bit count of the floating-point type, whichever is less. Additionally, this method generates uniformly distributed unsigned integers in the half-open range [0, 2 ** x), where ** is the exponentiation operator and x is the lesser ofbitCount
and the bit width of the integer type.For end users, however, the recommended spelling for a uniformly distributed numeric value is
See more…uniform()
; that method is overloaded to permit custom minimum and maximum values for the uniform distribution.Declaration
Swift
public protocol PRNG : class, IteratorProtocol, Sequence where Element : FixedWidthInteger & UnsignedInteger, SubSequence : Sequence, Element == SubSequence.Element
-
A floating-point type that supports elementary functions and a selection of special functions.
The
See more…Real
protocol provides a suitable basis for writing functions that work on any floating-point type that supports the required functions.Declaration
Swift
public protocol Real : Math, FloatingPoint
-
A signed numeric type that supports elementary functions.
The
See more…Math
protocol provides a suitable basis for writing functions that work on any real or complex floating-point type that supports the required functions.Declaration
Swift
public protocol Math : SignedNumeric