Skip to main content

Omni RNG: A unified, cross-backend random number generator built on the Array API standard.

Project description

Omni RNG (orng)

orng provides a thin wrapper over several Array API–compatible random number generators. It mirrors the 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

The core package only depends on the standard Python library:

pip install orng

Backends are optional extras that you can install on demand:

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.

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,), dtype=None)

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.

Project Layout

orng/
├── src/orng/
│   ├── __init__.py      # package exports
│   ├── _utils.py        # shared helpers (internal)
│   ├── orng.py          # RandomGenerator wrapper
│   └── backends/        # backend-specific implementations
└── README.md

Each backend class lives in its own module under orng/backends/, keeping the core facade compact and making optional dependencies easy to manage.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orng-0.1.0a7.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

orng-0.1.0a7-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file orng-0.1.0a7.tar.gz.

File metadata

  • Download URL: orng-0.1.0a7.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for orng-0.1.0a7.tar.gz
Algorithm Hash digest
SHA256 d774044e1fb5a1060d32c01ccd7d1adeb0c43ae7cef4800b1527dcb0db8a8f9e
MD5 57e63e5ba6cad8e10845d215621d066b
BLAKE2b-256 4ecc44cc2f045fc6e277b412519d20f09287e5923ca0c2c125aabc33ed6b0914

See more details on using hashes here.

Provenance

The following attestation bundles were made for orng-0.1.0a7.tar.gz:

Publisher: publish.yml on sequince-dev/orng

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file orng-0.1.0a7-py3-none-any.whl.

File metadata

  • Download URL: orng-0.1.0a7-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for orng-0.1.0a7-py3-none-any.whl
Algorithm Hash digest
SHA256 770171149277528fdb4c2965190a85c8f1be6987c53cc717dc502e10d233845b
MD5 ce929d573ac76f2a90fed43f87135874
BLAKE2b-256 63b5bdf7fba0b2c5063084385d1a61052943a986e6d1e2ed03f125e4f2a4a8f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for orng-0.1.0a7-py3-none-any.whl:

Publisher: publish.yml on sequince-dev/orng

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page