SPB Git

Repository Public

Xoshiro.swift 3.1 KB 77 lines swift

1import Foundation23/// xoshiro256** 1.0 (Blackman & Vigna, public domain): a fast, high-quality, seedable generator.4/// The 256-bit state is expanded from a 64-bit seed with SplitMix64, as the authors recommend,5/// so a given seed always produces the same stream on every platform.6public struct Xoshiro256StarStar: RandomNumberGenerator, Sendable, Hashable, Codable {7    public private(set) var state: [UInt64]89    public init(seed: UInt64) {10        var z = seed11        var s = [UInt64]()12        for _ in 0..<4 {13            z &+= 0x9E37_79B9_7F4A_7C1514            var x = z15            x = (x ^ (x >> 30)) &* 0xBF58_476D_1CE4_E5B916            x = (x ^ (x >> 27)) &* 0x94D0_49BB_1331_11EB17            s.append(x ^ (x >> 31))18        }19        state = s20    }2122    @inline(__always) private static func rotl(_ x: UInt64, _ k: UInt64) -> UInt64 { (x << k) | (x >> (64 - k)) }2324    public mutating func next() -> UInt64 {25        let result = Self.rotl(state[1] &* 5, 7) &* 926        let t = state[1] << 1727        state[2] ^= state[0]28        state[3] ^= state[1]29        state[1] ^= state[2]30        state[0] ^= state[3]31        state[2] ^= t32        state[3] = Self.rotl(state[3], 45)33        return result34    }3536    /// Uniform double in [0, 1) with 53 random bits.37    public mutating func nextUniform() -> Double { Double(next() >> 11) * 0x1.0p-53 }3839    /// Uniform double in the open interval (0, 1): (k + 0.5) / 2⁵³, never 0 or 1 (safe for inverse CDFs).40    public mutating func nextOpenUniform() -> Double { (Double(next() >> 11) + 0.5) * 0x1.0p-53 }4142    /// Standard normal deviate by inversion of the CDF (one uniform per deviate, monotone in the43    /// uniform — which keeps rank-correlation methods and antithetic pairs exact).44    public mutating func nextNormal() -> Double { SpecialFunctions.normalQuantile(nextOpenUniform()) }4546    /// Uniform integer in 0..<n (n > 0), unbiased (Lemire's rejection on the high bits).47    public mutating func nextInt(below n: Int) -> Int {48        precondition(n > 0)49        let bound = UInt64(n)50        var m = next().multipliedFullWidth(by: bound)51        if m.low < bound {52            let threshold = (0 &- bound) % bound53            while m.low < threshold { m = next().multipliedFullWidth(by: bound) }54        }55        return Int(m.high)56    }5758    /// Advances the state by 2¹²⁸ steps: use it to derive non-overlapping streams for parallel work.59    public mutating func jump() {60        let jumps: [UInt64] = [0x180E_C6D3_3CFD_0ABA, 0xD5A6_1266_F0C9_392C, 0xA958_2618_E03F_C9AA, 0x39AB_DC45_29B1_661C]61        var s0: UInt64 = 0, s1: UInt64 = 0, s2: UInt64 = 0, s3: UInt64 = 062        for j in jumps {63            for b in 0..<64 {64                if j & (1 << UInt64(b)) != 0 { s0 ^= state[0]; s1 ^= state[1]; s2 ^= state[2]; s3 ^= state[3] }65                _ = next()66            }67        }68        state = [s0, s1, s2, s3]69    }7071    /// Fisher–Yates shuffle driven by this generator.72    public mutating func shuffle<T>(_ a: inout [T]) {73        guard a.count > 1 else { return }74        for i in stride(from: a.count - 1, to: 0, by: -1) { a.swapAt(i, nextInt(below: i + 1)) }75    }76}77
Files main
Menu

Theme