RandomNumberGenerator

RandomNumberGenerator

RandomNumberGenerator()

A reproducible, fast, non-cryptographic random number generator suitable for parallel Monte Carlo simulations. A seed has to be set, which can be any u64 number (small numbers work just as well as large numbers).

Each thread or instance generating samples should use the same seed but a different stream_id, which is an instance counter starting at 0.

Methods

Name Description
__copy__ Copy the random number generator, so that the copy will generate the same sequence of random numbers.
__new__ Create a new random number generator with a given seed and stream_id
load Import a random number generator from a previously exported state
next Generate the next random unsigned 64-bit integer in the sequence.
next_float Generate the next random floating-point number in the sequence, uniformly distributed in the range [0, 1).
save Export the random number generator state as a bytes object of length 32, which can be imported again to restore the state.

__copy__

RandomNumberGenerator.__copy__() -> RandomNumberGenerator

Copy the random number generator, so that the copy will generate the same sequence of random numbers.

__new__

RandomNumberGenerator.__new__(seed: int, stream_id: int)

Create a new random number generator with a given seed and stream_id. For parallel runs, each thread or instance generating samples should use the same seed but a different stream_id.

Parameters

  • seed (int) The seed used to initialize the random number generator.
  • stream_id (int) The stream identifier for the random number generator.

load

RandomNumberGenerator.load(state: bytes) -> RandomNumberGenerator

Import a random number generator from a previously exported state. The state should be a bytes object of length 32.

Parameters

  • state (bytes) The serialized state to load.

next

RandomNumberGenerator.next() -> int

Generate the next random unsigned 64-bit integer in the sequence.

next_float

RandomNumberGenerator.next_float() -> float

Generate the next random floating-point number in the sequence, uniformly distributed in the range [0, 1).

save

RandomNumberGenerator.save() -> bytes

Export the random number generator state as a bytes object of length 32, which can be imported again to restore the state.