﻿# CuPy random state: seeding one GPU does not reset every device generator

Define CuPy random-generator ownership per device, distinguish global seeding from explicit RandomState instances and document replay limits across environments.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cupy-random-state-device-seed-scope/
Language: en

## Identify which generator a helper consumes

A preprocessing helper draws random masks through the global CuPy interface while a caller expects an explicit generator to control the experiment. The [RandomState reference](https://docs.cupy.dev/en/stable/reference/generated/cupy.random.RandomState.html) describes instance state and global instances used for different devices. Establish which object owns each random draw. Passing a generator to an outer function does not control a nested helper that ignores it and consumes unrelated global state instead.

## Seed the intended device scope

The [seed reference](https://docs.cupy.dev/en/stable/reference/generated/cupy.random.seed.html) states that global seeding resets the generator for the current device without affecting other devices. A call made before switching devices is not a complete initialization procedure for a multi-GPU program. Map logical workers and devices to their intended generator initialization. Record that mapping explicitly rather than assuming that every accelerator starts from a shared process-wide random sequence after a single setup call.

## Keep stateful call order visible

A generator advances as operations consume it. Adding a diagnostic sample or changing which helper executes first can change later random masks even when the initialization seed remains unchanged. Give independent consumers explicit ownership when their random decisions should remain independent of incidental call order. This is an application design choice, not a guarantee that arbitrary seeds produce statistically independent streams. Preserve the intended allocation of random decisions alongside the preprocessing algorithm.

## Do not promise portable sequence equality

The [CuPy random-sampling reference](https://docs.cupy.dev/en/stable/reference/random.html) documents differences from NumPy, lack of serializable random states and the absence of a generator-identity guarantee across major versions. Store the actual library environment and generator choice in a replay record. A shared numerical seed is insufficient evidence that a different backend or upgraded environment will reproduce identical draws. When exact sampled artifacts matter, retain those artifacts or an appropriate supported reconstruction procedure as part of experiment provenance.

## Test ownership with an intentional disturbance

Verify the expected device mapping, then insert a draw into a consumer that should be isolated and check whether unrelated outputs change. Use separate tests for distributional plausibility and exact replay. The guides on [random-state replay](/journal/data-guide-data-rng-state-replay/) and [distributed loader ownership](/journal/data-guide-distributed-shard-worker-overlap/) provide related context. Reproducibility requires a declared state-consumption contract; recording a seed without its generator, device scope and call structure explains only part of the experiment.
