Created
September 21, 2023 01:59
-
-
Save malonehedges/12ad0d46b21057cf6b7f48eed0efdc36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GameKit | |
public class SeededGenerator: RandomNumberGenerator { | |
public let seed: UInt64 | |
private let generator: GKMersenneTwisterRandomSource | |
public convenience init() { | |
self.init(seed: 0) | |
} | |
public init(seed: UInt64) { | |
self.seed = seed | |
generator = GKMersenneTwisterRandomSource(seed: seed) | |
} | |
public func next() -> UInt64 { | |
let a = Int32(generator.nextInt()) | |
let b = Int32(generator.nextInt()) | |
let upperBits = UInt64(UInt32(bitPattern: a)) << 32 | |
let lowerBits = UInt64(UInt32(bitPattern: b)) | |
return upperBits | lowerBits | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment