// LyrebirdRandom.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT /// A seedable scalar random source over Lyrebird's distribution family. /// /// Where ``TemporalStream`` / ``Pattern`` produce *sequences* and the noise /// generators produce *signals*, `LyrebirdRandom` answers the "give me one /// value" case: a single draw from a uniform range, a Gaussian, a Laplace, and /// so on. It owns a xorshift64 state, so a given `seed` produces a fully /// reproducible stream of draws — which is what musical use wants. /// /// ```swift /// var r = LyrebirdRandom(seed: 42) /// let freq: Double = r.exponentialRange(low: 200, high: 2000) // Hz, log-uniform /// let dur: Double = r.uniform(low: 0.1, high: 0.5) // seconds /// let jitter: Double = r.gauss(mean: 0, dev: 0.02) // ± around 0 /// ``` /// /// Each draw method maps to a ``DistributionKind`` case; the generic /// ``value(_:parX:parY:parA:parB:)`` reaches any distribution by kind. For /// throwaway draws with no seed bookkeeping, use ``shared``. /// /// Value semantics: copying a `LyrebirdRandom` forks its stream. Not /// thread-safe — give each thread its own instance (or use ``shared``, which is /// synchronized). public struct LyrebirdRandom: Sendable { /// The live xorshift64 state. Exposed so callers can snapshot/restore a /// draw position; mutated by every draw. public var state: UInt64 /// Create a reproducible source from an explicit seed. /// - Parameter seed: Any `UInt64`; normalised to a non-zero xorshift state. public init(seed: UInt64) { self.state = lyrebirdSeedXorshift64(from: seed) } /// Create a source seeded from system entropy (uptime nanoseconds) — draws /// are not reproducible across runs. Prefer ``init(seed:)`` when you need /// repeatability. public init() { self.state = lyrebirdSeedXorshift64(from: nil) } // MARK: - Generic /// Draw one sample from `kind`, advancing this source's state. /// - Parameters: /// - kind: The distribution to sample. /// - parX: First parameter (per ``DistributionKind``'s table). /// - parY: Second parameter. Ignored by single-parameter distributions. /// - parA: Third parameter (beta `alpha` / Cauchy positive-only). `nil` = default. /// - parB: Fourth parameter (beta `beta`). `nil` = default. /// - Returns: One sample. public mutating func value( _ kind: DistributionKind, parX: Double, parY: Double = 1, parA: Double? = nil, parB: Double? = nil ) -> Double { kind.sample(parX: parX, parY: parY, parA: parA, parB: parB, using: &state) } // MARK: - Uniform family /// A uniform sample in `[0, 1]`. public mutating func unit() -> Double { DistributionKind.uniform.sample(parX: 0, parY: 1, using: &state) } /// Uniform (flat) sample in `[low, high]`. public mutating func uniform(low: Double, high: Double) -> Double { DistributionKind.uniform.sample(parX: low, parY: high, using: &state) } /// Sample in `[low, high]` biased toward `low` (squared uniform). public mutating func lowBiased(low: Double, high: Double) -> Double { DistributionKind.lpRand.sample(parX: low, parY: high, using: &state) } /// Sample in `[low, high]` biased toward `high` (mirrored squared uniform). public mutating func highBiased(low: Double, high: Double) -> Double { DistributionKind.hpRand.sample(parX: low, parY: high, using: &state) } /// Sample in `[low, high]` biased toward the midpoint (mean of two uniforms). public mutating func meanBiased(low: Double, high: Double) -> Double { DistributionKind.meanRand.sample(parX: low, parY: high, using: &state) } /// Log-uniform sample in `[low, high]`; both bounds must be `> 0`. public mutating func exponentialRange(low: Double, high: Double) -> Double { DistributionKind.expRand.sample(parX: low, parY: high, using: &state) } // MARK: - Single-parameter families /// Standard exponential; `density` is the rate λ (`> 0`). Output `≥ 0`. public mutating func exponential(density: Double) -> Double { DistributionKind.exponential.sample(parX: density, using: &state) } /// Gamma distribution; `shape` is `k` (`> 0`). `k = 1` is exponential. public mutating func gamma(shape: Double) -> Double { DistributionKind.gamma.sample(parX: shape, using: &state) } /// Laplace (double-exponential), symmetric about 0; `scale` is dispersion `b`. public mutating func laplace(scale: Double = 1) -> Double { DistributionKind.laplace.sample(parX: scale, using: &state) } /// Asymmetric Laplace (right tail heavier, p = 0.7); `scale` is dispersion `b`. public mutating func asymmetricLaplace(scale: Double = 1) -> Double { DistributionKind.alaplace.sample(parX: scale, using: &state) } /// Half-cosine bell centred at 0; output ≈ `[−d/π, d/π]`. public mutating func halfCosine(dispersion: Double = 1) -> Double { DistributionKind.hcos.sample(parX: dispersion, using: &state) } /// Logistic distribution centred at 0 (heavier tails than Gaussian); `scale` is `s`. public mutating func logistic(scale: Double = 1) -> Double { DistributionKind.logistic.sample(parX: scale, using: &state) } /// Poisson counts; `mean` is λ (clamped to `[0.001, 30]`). Integer-valued. public mutating func poisson(mean: Double) -> Double { DistributionKind.poisson.sample(parX: mean, using: &state) } /// U-shaped arcsin distribution; output in `[−dispersion, dispersion]`. public mutating func arcsin(dispersion: Double = 1) -> Double { DistributionKind.arcsin.sample(parX: dispersion, using: &state) } // MARK: - Multi-parameter families /// Beta distribution mapped to `[low, high]`. `alpha`/`beta` shape it /// (`alpha == beta` symmetric; both `< 1` bimodal; `1, 1` uniform). public mutating func beta( low: Double, high: Double, alpha: Double = 2, beta: Double = 2 ) -> Double { DistributionKind.betaRand.sample(parX: low, parY: high, parA: alpha, parB: beta, using: &state) } /// Cauchy distribution; heavy-tailed. `positiveOnly` folds onto the positive axis. public mutating func cauchy( mean: Double = 0, spread: Double = 1, positiveOnly: Bool = false ) -> Double { DistributionKind.cauchy.sample(parX: mean, parY: spread, parA: positiveOnly ? 1 : 0, using: &state) } /// Gaussian (normal) distribution; `dev` is the standard deviation σ. public mutating func gauss(mean: Double = 0, dev: Double = 1) -> Double { DistributionKind.gauss.sample(parX: dev, parY: mean, using: &state) } }