Files
Lyrebird/Sources/Lyrebird/Distributions/SharedRandom.swift
T
josh 62b2f5dbec v1.5.0 release: LyrebirdRandom + Live Scratch editor
LyrebirdRandom scalar distribution sampling (seedable value type + shared
Random) with DistributionKind.sample dispatcher. Live Scratch editor: rename to
LyrebirdScratch, App Support artifact staging, Format Code (⌥⌘F), Render to File
(⌥⌘R offline CAF), line-number gutter, manual completion, Foundation in snippets,
scroll-jump fix, scheduling/NRT docs.
2026-07-22 07:53:12 -07:00

139 lines
5.3 KiB
Swift

// SharedRandom.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Process-wide, thread-safe scalar random source for throwaway draws where you
/// don't want to thread a ``LyrebirdRandom`` through your code. Reached via
/// ``LyrebirdRandom/shared``.
///
/// ```swift
/// let x: Double = LyrebirdRandom.shared.uniform(low: 0, high: 1)
/// let g: Double = LyrebirdRandom.shared.gauss(mean: 0, dev: 1)
/// ```
///
/// System-seeded, so its stream is **not reproducible** across runs, and its
/// interleaving across threads is non-deterministic — every draw locks a shared
/// state. When you need repeatable draws (e.g. a rendered score), use an owned
/// `LyrebirdRandom(seed:)` instead. Call ``reseed(_:)`` to pin the shared stream
/// for a test.
public final class SharedRandom: @unchecked Sendable {
private var gen: LyrebirdRandom
private let lock: NSLock = NSLock()
init() {
self.gen = LyrebirdRandom()
}
/// Replace the shared state with one seeded from `seed`. Mainly for tests
/// that need the shared source to be reproducible.
public func reseed(_ seed: UInt64) {
lock.lock(); defer { lock.unlock() }
gen = LyrebirdRandom(seed: seed)
}
private func locked(_ body: (inout LyrebirdRandom) -> Double) -> Double {
lock.lock(); defer { lock.unlock() }
return body(&gen)
}
/// See ``LyrebirdRandom/value(_:parX:parY:parA:parB:)``.
public func value(
_ kind: DistributionKind,
parX: Double,
parY: Double = 1,
parA: Double? = nil,
parB: Double? = nil
) -> Double {
locked { $0.value(kind, parX: parX, parY: parY, parA: parA, parB: parB) }
}
/// See ``LyrebirdRandom/unit()``.
public func unit() -> Double { locked { $0.unit() } }
/// See ``LyrebirdRandom/uniform(low:high:)``.
public func uniform(low: Double, high: Double) -> Double { locked { $0.uniform(low: low, high: high) } }
/// See ``LyrebirdRandom/lowBiased(low:high:)``.
public func lowBiased(low: Double, high: Double) -> Double { locked { $0.lowBiased(low: low, high: high) } }
/// See ``LyrebirdRandom/highBiased(low:high:)``.
public func highBiased(low: Double, high: Double) -> Double { locked { $0.highBiased(low: low, high: high) } }
/// See ``LyrebirdRandom/meanBiased(low:high:)``.
public func meanBiased(low: Double, high: Double) -> Double { locked { $0.meanBiased(low: low, high: high) } }
/// See ``LyrebirdRandom/exponentialRange(low:high:)``.
public func exponentialRange(low: Double, high: Double) -> Double { locked { $0.exponentialRange(low: low, high: high) } }
/// See ``LyrebirdRandom/exponential(density:)``.
public func exponential(density: Double) -> Double { locked { $0.exponential(density: density) } }
/// See ``LyrebirdRandom/gamma(shape:)``.
public func gamma(shape: Double) -> Double { locked { $0.gamma(shape: shape) } }
/// See ``LyrebirdRandom/laplace(scale:)``.
public func laplace(scale: Double = 1) -> Double { locked { $0.laplace(scale: scale) } }
/// See ``LyrebirdRandom/asymmetricLaplace(scale:)``.
public func asymmetricLaplace(scale: Double = 1) -> Double { locked { $0.asymmetricLaplace(scale: scale) } }
/// See ``LyrebirdRandom/halfCosine(dispersion:)``.
public func halfCosine(dispersion: Double = 1) -> Double { locked { $0.halfCosine(dispersion: dispersion) } }
/// See ``LyrebirdRandom/logistic(scale:)``.
public func logistic(scale: Double = 1) -> Double { locked { $0.logistic(scale: scale) } }
/// See ``LyrebirdRandom/poisson(mean:)``.
public func poisson(mean: Double) -> Double { locked { $0.poisson(mean: mean) } }
/// See ``LyrebirdRandom/arcsin(dispersion:)``.
public func arcsin(dispersion: Double = 1) -> Double { locked { $0.arcsin(dispersion: dispersion) } }
/// See ``LyrebirdRandom/beta(low:high:alpha:beta:)``.
public func beta(
low: Double,
high: Double,
alpha: Double = 2,
beta: Double = 2
) -> Double {
locked { $0.beta(low: low, high: high, alpha: alpha, beta: beta) }
}
/// See ``LyrebirdRandom/cauchy(mean:spread:positiveOnly:)``.
public func cauchy(
mean: Double = 0,
spread: Double = 1,
positiveOnly: Bool = false
) -> Double {
locked { $0.cauchy(mean: mean, spread: spread, positiveOnly: positiveOnly) }
}
/// See ``LyrebirdRandom/gauss(mean:dev:)``.
public func gauss(mean: Double = 0, dev: Double = 1) -> Double {
locked { $0.gauss(mean: mean, dev: dev) }
}
}
extension LyrebirdRandom {
/// Process-wide thread-safe source for quick, non-reproducible draws.
/// See ``SharedRandom``.
public static let shared: SharedRandom = SharedRandom()
}
/// Global shorthand for the process-wide shared random source
/// (``LyrebirdRandom/shared``) — the common non-seeded case.
///
/// ```swift
/// let pan: Double = Random.uniform(low: -1, high: 1)
/// let freq: Double = Random.exponentialRange(low: 200, high: 2000)
/// ```
///
/// Draws are **not reproducible** across runs; for a repeatable seeded stream use
/// `LyrebirdRandom(seed:)`. Capitalized to avoid shadowing C's `rand()`.
public let Random: SharedRandom = LyrebirdRandom.shared