warp.rand_init#
- warp.rand_init(seed: int32) uint32#
Kernel
Python
Initialize a random number generator (RNG) state from a seed.
Warp’s RNG is a stateless PCG hash (Jarzynski & Olano, 2020):
rand_initturns a seed into a 32-bitstatethat is passed to therand*andsample_*built-ins. In a kernel, these built-ins advancestatein place, so repeated calls on the same local variable return different values. When called directly from the Python scope, they do not modifystate: callingwp.randf(state)twice with the samestatereturns the same value both times. Results are deterministic and reproducible for a given seed and call order on every device.stateis just a local value, not a persistent generator object: it lives only for the duration of the kernel invocation and does not carry over between launches. Re-launching with the same seed and per-thread offsets reproduces the same sequences. To draw different sequences across launches, change the seed or include a launch-specific value in therand_init(seed, offset)offset. See Avoiding Correlated Sequences for examples.For parallel kernels, prefer the
rand_init(seed, offset)overload with a unique per-threadoffsetso each thread draws a distinct sequence. See the Random Number Generation user guide section for more details.- Parameters:
seed – Seed value used to derive the initial state.
- Returns:
A 32-bit unsigned integer holding the initial RNG state.
- warp.rand_init(seed: int32, offset: int32) uint32
Kernel
Python
Initialize a random number generator (RNG) state from a seed and an offset.
Both
seedandoffsetare hashed into the returned state. This is the recommended constructor for parallel kernels: shareseedacross the launch and pass a unique per-threadoffset(typicallywp.tid()) so each thread starts from a distinct RNG state and avoids sharing the same sequence:@wp.kernel def sample_kernel(seed: int, out: wp.array[float]): tid = wp.tid() rng = wp.rand_init(seed, tid) out[tid] = wp.randf(rng)
See the Random Number Generation user guide section for the RNG model and guidance on avoiding correlated sequences.
- Parameters:
seed – Seed shared across a kernel launch.
offset – Per-thread offset selecting a distinct sequence.
- Returns:
A 32-bit unsigned integer holding the initial RNG state.