From efd9905f5b08da0a913b5730180f6394d56549a5 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Fri, 23 Aug 2024 09:13:42 +1000 Subject: [PATCH] generalise shared prng functionality to interfaces --- Sources/Voxelotl/Random/PCG32Random.swift | 9 +++++---- Sources/Voxelotl/Random/RandomProvider.swift | 15 +++++++++++++++ Sources/Voxelotl/Random/Xoroshiro128.swift | 12 +++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Sources/Voxelotl/Random/PCG32Random.swift b/Sources/Voxelotl/Random/PCG32Random.swift index 3462663..f1c7532 100644 --- a/Sources/Voxelotl/Random/PCG32Random.swift +++ b/Sources/Voxelotl/Random/PCG32Random.swift @@ -1,5 +1,6 @@ -public struct PCG32Random: RandomProvider { +public struct PCG32Random: RandomProvider, RandomStateAccess { public typealias Output = UInt32 + public typealias StateType = (UInt64, UInt64) public static var min: UInt32 { .min } public static var max: UInt32 { .max } @@ -19,12 +20,12 @@ public struct PCG32Random: RandomProvider { self._inc = 0xDA3E39CB94B95BDB } - public init(seed: UInt64, sequence: UInt64) { + public init(state: (UInt64, UInt64)) { self.init() - self.seed(state: _state, sequence: sequence) + self.reset(state: state.0, sequence: state.1) } - public mutating func seed(state: UInt64, sequence: UInt64) { + public mutating func reset(state: UInt64, sequence: UInt64) { self._state = 0 self._inc = sequence << 1 | 0x1 _ = next() diff --git a/Sources/Voxelotl/Random/RandomProvider.swift b/Sources/Voxelotl/Random/RandomProvider.swift index 110a301..f4cdbb4 100644 --- a/Sources/Voxelotl/Random/RandomProvider.swift +++ b/Sources/Voxelotl/Random/RandomProvider.swift @@ -6,3 +6,18 @@ public protocol RandomProvider { mutating func next() -> Output } + +public protocol RandomSeedable { + associatedtype SeedType: FixedWidthInteger + + init(seed: SeedType) + mutating func seed(_ value: SeedType) +} + +public protocol RandomStateAccess { + associatedtype StateType + + var state: StateType { get set } + + init(state: StateType) +} diff --git a/Sources/Voxelotl/Random/Xoroshiro128.swift b/Sources/Voxelotl/Random/Xoroshiro128.swift index e6eb016..305a757 100644 --- a/Sources/Voxelotl/Random/Xoroshiro128.swift +++ b/Sources/Voxelotl/Random/Xoroshiro128.swift @@ -1,5 +1,7 @@ -struct Xoroshiro128Plus: RandomProvider { +struct Xoroshiro128Plus: RandomProvider, RandomSeedable, RandomStateAccess { public typealias Output = UInt64 + public typealias SeedType = UInt64 + public typealias StateType = (UInt64, UInt64) static public var min: UInt64 { .min } static public var max: UInt64 { .max } @@ -36,8 +38,10 @@ struct Xoroshiro128Plus: RandomProvider { } } -struct Xoroshiro128PlusPlus: RandomProvider { +struct Xoroshiro128PlusPlus: RandomProvider, RandomSeedable, RandomStateAccess { public typealias Output = UInt64 + public typealias SeedType = UInt64 + public typealias StateType = (UInt64, UInt64) static public var min: UInt64 { .min } static public var max: UInt64 { .max } @@ -74,8 +78,10 @@ struct Xoroshiro128PlusPlus: RandomProvider { } } -struct Xoroshiro128StarStar: RandomProvider { +struct Xoroshiro128StarStar: RandomProvider, RandomSeedable, RandomStateAccess { public typealias Output = UInt64 + public typealias SeedType = UInt64 + public typealias StateType = (UInt64, UInt64) static public var min: UInt64 { .min } static public var max: UInt64 { .max }