mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-03 13:11:33 +00:00
basis for random subsystem
This commit is contained in:
24
Sources/Voxelotl/Random/Arc4Random.swift
Normal file
24
Sources/Voxelotl/Random/Arc4Random.swift
Normal file
@ -0,0 +1,24 @@
|
||||
import Foundation
|
||||
|
||||
public class Arc4Random: RandomProvider {
|
||||
public typealias Output = UInt32
|
||||
|
||||
public static var min: UInt32 { 0x00000000 }
|
||||
public static var max: UInt32 { 0xFFFFFFFF }
|
||||
|
||||
private init() {}
|
||||
public static let instance = Arc4Random()
|
||||
|
||||
public func stir() {
|
||||
arc4random_stir()
|
||||
}
|
||||
|
||||
public func next() -> UInt32 {
|
||||
arc4random()
|
||||
}
|
||||
|
||||
public func next(in bound: Range<Int>) -> Int {
|
||||
assert(bound.upperBound <= Self.max)
|
||||
return bound.lowerBound + Int(arc4random_uniform(UInt32(bound.upperBound)))
|
||||
}
|
||||
}
|
8
Sources/Voxelotl/Random/RandomProvider.swift
Normal file
8
Sources/Voxelotl/Random/RandomProvider.swift
Normal file
@ -0,0 +1,8 @@
|
||||
public protocol RandomProvider {
|
||||
associatedtype Output: FixedWidthInteger
|
||||
|
||||
static var min: Output { get }
|
||||
static var max: Output { get }
|
||||
|
||||
mutating func next() -> Output
|
||||
}
|
Reference in New Issue
Block a user