Implementation:Haosulab ManiSkill BatchedRNG
| Knowledge Sources | |
|---|---|
| Domains | Robotics, Simulation, Randomization |
| Last Updated | 2026-02-15 08:00 GMT |
Overview
Concrete tool for managing a batch of independent random number generators to ensure reproducible randomization across CPU and GPU simulators.
Description
The BatchedRNG class extends np.random.RandomState to manage a collection of independent random number generators (one per parallel environment). This ensures that randomization behavior is identical between CPU and GPU simulation backends when given the same seeds.
Key design:
- Each parallel environment gets its own
np.random.RandomStateinstance, stored in therngslist. from_seeds()class method creates the batch from a list of integer seeds.from_rngs()class method creates from pre-existing RNG instances.- Indexing with
[]returns a sub-batch or individual RNG. - Assignment with
[]=replaces RNGs at specific indices. - Any method call (e.g.,
.uniform(),.normal()) is automatically delegated to each individual RNG and the results are stacked into a numpy array. This is achieved via__getattribute__override.
Usage
Used internally by ManiSkill environments for seeded randomization during reset. Particularly important for ensuring that CPU-based and GPU-based environments produce identical random configurations from the same seed.
Code Reference
Source Location
- Repository: Haosulab_ManiSkill
- File: mani_skill/envs/utils/randomization/batched_rng.py
Signature
class BatchedRNG(np.random.RandomState):
def __init__(self, rngs: list): ...
@classmethod
def from_seeds(cls, seeds: list[int], backend: str = "numpy:random_state") -> "BatchedRNG": ...
@classmethod
def from_rngs(cls, rngs: list) -> "BatchedRNG": ...
def __getitem__(self, idx: Union[int, list[int], np.ndarray]) -> Union["BatchedRNG", np.random.RandomState]: ...
def __setitem__(self, idx, value) -> None: ...
Import
from mani_skill.envs.utils.randomization.batched_rng import BatchedRNG
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| seeds | list[int] | Yes | List of integer seeds, one per parallel environment |
| backend | str | No | RNG backend type (default: "numpy:random_state") |
Outputs
| Name | Type | Description |
|---|---|---|
| BatchedRNG | BatchedRNG | A batched RNG that delegates calls to per-env RNGs |
| method results | np.ndarray | Stacked results from calling any RandomState method |
Usage Examples
Basic Usage
from mani_skill.envs.utils.randomization.batched_rng import BatchedRNG
# Create batched RNG for 4 parallel environments
rng = BatchedRNG.from_seeds([42, 43, 44, 45])
# Generate uniform random numbers for each env
values = rng.uniform(low=0, high=1, size=(3,)) # shape (4, 3)
# Index into a subset
sub_rng = rng[[0, 2]] # BatchedRNG with 2 RNGs