rust-random / book

The Rust Rand Book
Other
49 stars 19 forks source link

Document how to run RNGs in parallel #45

Closed vks closed 2 years ago

vks commented 3 years ago

There was some discussion in https://github.com/rust-random/rand/issues/997:

Perhaps you mean that one can safely seed multiple independent streams. For this purpose we specifically chose RNGs supporting ~128-bit seeds or larger. There are some potential issues with PCG streams being too similar, so not all of those RNGs are suitable (but the ones with 64-bit output already use 128-bit internal state).

You also need to ensure the seeding mechanism does not create similarities in state — some people recommend using a different type of RNG for seeding each thread's generator; I'd recommend using a (near) crypto-grade generator such as ChaCha since any such similarities would violate the requirements on a crypto generator. Seeding one RNG from another is easy; see the book.

Use case is you want to split a Monte-Carlo simulation over N processors. This can be done with skip-ahead, e.g. with ChaCha, but typically we recommend using a cryptographic master generator to seed each parallel generator. Our ChaCha implementation (and most I believe) uses a 64-bit counter, which likely isn't enough for parallel usage, however it uses a 256-bit seed. The parallel generator (ChaCha or other) still needs to support at least 128-bits of state and independent streams (so not recommended to use PCG), but a 64-bit period is acceptable.

I think it would be good to document how to use StdRng (with random seeds) and maybe rand_xoshiro (with skipping) in parallel.

dhardy commented 2 years ago

This topic came up again.