Skip to main content

Omni RNG (orng)

orng provides a thin wrapper over several Array API–compatible random number generators. It mirrors a subset of the numpy.random.Generator API:

  • random
  • uniform
  • normal
  • choice
  • gamma

letting you pick the underlying backend at runtime. The following backends are currently supported:

  • numpy
  • torch
  • cupy
  • jax

Installation

orng can be install from PyPI using pip:

pip install orng

Backends are optional extras that you can install as needed:

pip install "orng[numpy]"   # NumPy RNG support
pip install "orng[torch]"   # PyTorch RNG support
pip install "orng[cupy]"    # CuPy RNG support
pip install "orng[jax]"     # JAX RNG support

You can also combine extras, e.g. pip install "orng[numpy,torch]".

Quick Start

from orng import RandomGenerator

rng = RandomGenerator(backend="numpy", seed=42)
samples = rng.normal(loc=0.0, scale=1.0, size=5)
uniform = rng.uniform(low=-1.0, high=1.0, size=(2, 2))

The backend module is imported lazily. If the requested library is missing, RandomGenerator will raise an informative ImportError that points to the matching extra.

Saving and restoring state

RandomGenerator exposes a versioned state mapping for checkpointing:

state = rng.state_dict()

restored = RandomGenerator.from_state_dict(state)
assert restored.backend == rng.backend

An existing generator can also be reset in place:

rng.load_state_dict(state)

The returned mapping is detached from the live generator. It contains native backend arrays where applicable.

Functional Backend API

For JAX and other functional workflows, orng also provides a pure API in orng.functional:

from orng.functional import create_functional_backend

backend = create_functional_backend("numpy")
state = backend.init_state(seed=42, generator=None)

x, state = backend.normal(state, loc=0.0, scale=1.0, size=(4,), dtype=None)
y, state = backend.uniform(state, low=-1.0, high=1.0, size=(2, 2), dtype=None)

Every sampling call takes an explicit state and returns (sample, next_state). This avoids mutable RNG objects inside compiled code.

By default this API is pure (pure=True). On stateful backends (numpy, torch, and cupy) this snapshots RNG state each call. For lower overhead on those backends, you can opt into a trusted mutable fast path with pure=False:

backend = create_functional_backend("numpy", pure=False)
state = backend.init_state(seed=42, generator=None)  # numpy.random.Generator
x, state = backend.normal(state, loc=0.0, scale=1.0, size=(4,))

The JAX functional backend is always pure and does not support pure=False.

Supported functional methods:

  • random
  • uniform
  • normal
  • choice
  • gamma

JAX Compilation Example

import jax
import jax.numpy as jnp
from orng.functional import create_functional_backend

backend = create_functional_backend("jax")
state = backend.init_state(seed=0, generator=None)

@jax.jit
def step(key):
    sample, next_key = backend.normal(
        key, loc=0.0, scale=1.0, size=(8,), dtype=jnp.float32
    )
    return sample, next_key

sample, state = step(state)

Functional State Reference

The functional API follows the native conventions of each backend rather than introducing a wrapper state type.

init_state(seed=..., generator=...) accepts backend-specific generator inputs:

Backend generator argument
numpy numpy.random.Generator
torch torch.Generator
cupy cupy.random.Generator
jax JAX PRNG key array, typically from jax.random.key(...)

If generator=None, ORNG creates a new backend-native state from seed. If seed=None, the backend chooses a fresh random seed using its usual behavior.

The state value passed into random, uniform, normal, choice, and gamma also matches the backend:

Backend pure=True state pure=False state
numpy NumPy bit-generator state dict numpy.random.Generator
torch TorchFunctionalState torch.Generator
cupy CuPy bit-generator state dict cupy.random.Generator
jax JAX PRNG key array not supported

For example, NumPy in pure mode snapshots and returns a bit-generator state dictionary each call, while pure=False threads a numpy.random.Generator through the same functional interface. JAX always uses and returns a PRNG key.

Backend State Reference

When you pass the optional generator argument to RandomGenerator, the expected object depends on the backend:

Backend Generator argument
numpy numpy.random.Generator
torch torch.Generator
cupy cupy.random.Generator
jax jax.random.KeyArray (from jax.random.key)

This lets you wrap an existing RNG/key instead of seeding a new one.

Citing

If you find orng useful in your work, please cite the corresponding DOI.

Metadata

Release files for orng 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for orng 0.2.1
File Size Uploaded
orng-0.2.1.tar.gz 25.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for orng 0.2.1
File Interpreter ABI Platform
orng-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 43.5 kB

Release files / orng-0.2.1.tar.gz

Download URL orng-0.2.1.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fc9025c629bda6a1835b0b178c471183dedd2430b0b7bc70de35e3626437c325
BLAKE2b-256 checksum
How to use checksums
3d82e0ae90bbe9a5318869b5a65546260ce8e1bdf33191a5a84045f6d67eaa70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.

Transparency log

Release files / orng-0.2.1-py3-none-any.whl

Download URL orng-0.2.1-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8d98fc058ae7e8a516405235590c95f1dd3916c39f28893cc769bf0adb9f90f
BLAKE2b-256 checksum
How to use checksums
763e0fa5690a3a8303d821a46fd35691b1b6f8a9c06b97b41847ca41aed72e90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page