diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2020-12-19 10:33:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-19 10:33:55 -0700 |
commit | 811f9eea3b105919846760a6eca4edd22cc11c1d (patch) | |
tree | 9b1ff648a2a187c3c409c27fa97abb16427e863c | |
parent | 3095b43db1831d876915bb581078e8d11c9febfb (diff) | |
parent | bfbe56caeb056456662309b33510f6b5531d3973 (diff) | |
download | numpy-811f9eea3b105919846760a6eca4edd22cc11c1d.tar.gz |
Merge pull request #18014 from ev-br/bit_gen_doc
DOC: random: add some examples for SeedSequence
-rw-r--r-- | doc/source/reference/random/bit_generators/index.rst | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/doc/source/reference/random/bit_generators/index.rst b/doc/source/reference/random/bit_generators/index.rst index 315657172..6f8cf02ca 100644 --- a/doc/source/reference/random/bit_generators/index.rst +++ b/doc/source/reference/random/bit_generators/index.rst @@ -105,6 +105,44 @@ If you need to generate a good seed "offline", then ``SeedSequence().entropy`` or using ``secrets.randbits(128)`` from the standard library are both convenient ways. +If you need to run several stochastic simulations in parallel, best practice +is to construct a random generator instance for each simulation. +To make sure that the random streams have distinct initial states, you can use +the `spawn` method of `~SeedSequence`. For instance, here we construct a list +of 12 instances: + +.. code-block:: python + + from numpy.random import PCG64, SeedSequence + + # High quality initial entropy + entropy = 0x87351080e25cb0fad77a44a3be03b491 + base_seq = SeedSequence(entropy) + child_seqs = base_seq.spawn(12) # a list of 12 SeedSequences + generators = [PCG64(seq) for seq in child_seqs] + +.. end_block + + +An alternative way is to use the fact that a `~SeedSequence` can be initialized +by a tuple of elements. Here we use a base entropy value and an integer +``worker_id`` + +.. code-block:: python + + from numpy.random import PCG64, SeedSequence + + # High quality initial entropy + entropy = 0x87351080e25cb0fad77a44a3be03b491 + sequences = [SeedSequence((entropy, worker_id)) for worker_id in range(12)] + generators = [PCG64(seq) for seq in sequences] + +.. end_block + +Note that the sequences produced by the latter method will be distinct from +those constructed via `~SeedSequence.spawn`. + + .. autosummary:: :toctree: generated/ |