use arc4random to seed non-csprng, fleshes out random subsystem

This commit is contained in:
2024-08-22 03:37:29 +10:00
parent 394e340f09
commit cb2ffe78a4
6 changed files with 83 additions and 15 deletions

View 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
}
}