A pseudo-random number generator written in Swift. Implements the Mersenne Twister (MT) algorithm as described here.
Implementation of the MT algorithm for 32-bit integers.
Wrapper around the MT implementation that provides random values for several types:
- UInt32
- UInt64
- Float
- Double
Provides both uniform and Gaussian (normal) distributions for Float and Double.
Use Squall
if parallel execution speed is not important. It is a global singleton with a serial queue.
let queues: [DispatchQueue] = // N DispatchQueues
for i in 0 ..< N {
queues[i].async {
// do stuff with Squall.random()
}
}
Use Gust
for parallel, independent PRNG. NOTE that a unique offset must be given given if multiple instances are created in close temporal proximity in order to preserve independence of results.
let queues: [DispatchQueue] = // N DispatchQueues
for i in 0 ..< N {
queues[i].async {
let g_i = Gust(offset: UInt32(i))
// do stuff with g_i.random()
}
}
- Updated package.swift for use in 5.0+ projects (ht @heckj)
- Fixed two integer overflow exceptions which could occur during initialization (ht @barrypress).
- Fixed typo in Mersenne generator (ht @barrypress).
- Added OS guards around
Squall
becauseDispatchQueue
is not always available on Linux systems. - Added conformance to
RandomNumberGenerator
toGust
so that it can be used with the unified Swift 4.2 random methods.
- Changed instances of
M_PI
toDouble.pi
andFloat.pi
- credit to @jmmaloney4
- Changed
safeMultiply()
todiscardMultiply
to better describe what the function does. - Added a serial queue to
Squall
for thread safety. - Added
Gust
: a mirror ofSquall
that is not thread safe but allows parallel execution.
- Updated for Swift 3.1
- Removed
Squall.randomData()
because the syntax forUnsafeMutablePointer
changed.
- Initial Release