A lightweight Swift implementation of the default seedrandom(seed) algorithm used in JavaScript.
This package provides deterministic, seed-based random number generation compatible with the default generator from the popular JavaScript library seedrandom.
The primary goal of this project is cross-platform reproducibility — allowing Swift applications (Metal, SwiftUI, etc.) to generate the same seeded random streams used in JavaScript environments such as Three.js, React-Three-Fiber, and other WebGL workflows.
This makes it possible to reproduce procedural scenes, simulations, or generative effects identically between web and native platforms.
• Deterministic seeded random number generation
• Compatible with JavaScript seedrandom(seed) default generator
• Pure Swift implementation (no dependencies)
• Distributed as a Swift Package Manager library
• Suitable for procedural graphics, simulations, generative art, and deterministic testing
Add the package to your project using Swift Package Manager.
- Open your project
- Select File → Add Package Dependencies
- Enter the repository URL
https://github.com/PhysisVerse/seedrandom-swift
- Select the latest version and add the package to your target.
Import the module:
import SeedRandomCreate a seeded random generator:
let rng = SeedRandom("hello.")Generate deterministic random values:
let value = rng.nextDouble()Calling nextDouble() repeatedly produces the same sequence for the same seed.
Creates a deterministic random number generator.
Example:
let rng = SeedRandom("liquid-randomness")Returns a deterministic floating-point number in the range:
0 ≤ value < 1
Example:
let value = rng.nextDouble()Equivalent JavaScript usage:
const rng = seedrandom("seed")
rng()Returns a 32-bit precision floating-point number.
Example:
let value = rng.quick()Equivalent JavaScript usage:
rng.quick()Returns a signed 32-bit integer.
Example:
let value = rng.int32()Equivalent JavaScript usage:
rng.int32()import SeedRandom
let rng = SeedRandom("example-seed")
for _ in 0..<5 {
print(rng.nextDouble())
}Running this code will always produce the same sequence for the same seed.
This library is useful when deterministic randomness is required across different platforms.
Examples include:
• Procedural graphics and shaders • Generative art • Game logic • Simulation systems • Cross-platform rendering pipelines • Reproducing WebGL scenes in native Metal or SwiftUI applications
This package implements the default ARC4-based generator used by seedrandom.js.
Only the core generator is included. Alternative algorithms distributed with the original project (Alea, xor4096, xorshift, etc.) are not implemented here.
The focus of this package is compatibility with the default usage pattern:
const rng = seedrandom(seed)
rng()This project is a Swift implementation inspired by the original seedrandom JavaScript library.
Original project:
https://github.com/davidbau/seedrandom
Copyright © David Bau
The original library is released under the MIT License.
This Swift implementation re-creates the deterministic generator behavior for use in native Apple platform applications.