mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-03 13:11:33 +00:00
use arc4random to seed non-csprng, fleshes out random subsystem
This commit is contained in:
20
Sources/Voxelotl/Random/RandomRange.swift
Normal file
20
Sources/Voxelotl/Random/RandomRange.swift
Normal file
@ -0,0 +1,20 @@
|
||||
public extension RandomProvider where Output: BinaryInteger {
|
||||
mutating func next(in range: Range<Int>) -> Int {
|
||||
range.lowerBound + self.next(in: range.upperBound - range.lowerBound)
|
||||
}
|
||||
|
||||
mutating func next(in range: ClosedRange<Int>) -> Int {
|
||||
range.lowerBound + self.next(in: range.upperBound - range.lowerBound + 1)
|
||||
}
|
||||
|
||||
mutating func next(in bound: Int) -> Int {
|
||||
assert(Self.min == 0)
|
||||
assert(Self.max >= bound)
|
||||
let threshold = Int(Self.max % Output(bound))
|
||||
var result: Int
|
||||
repeat {
|
||||
result = Int(truncatingIfNeeded: self.next())
|
||||
} while result < threshold
|
||||
return result % bound
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user