-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: use SeedSequence instead of seed() #13780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
doc/source/reference/random/bit_generators/bitgenerators.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
:orphan: | ||
|
||
BitGenerator | ||
------------ | ||
|
||
.. currentmodule:: numpy.random.bit_generator | ||
|
||
.. autosummary:: | ||
:toctree: generated/ | ||
|
||
BitGenerator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,49 @@ | ||
.. _bit_generator: | ||
|
||
.. currentmodule:: numpy.random | ||
|
||
Bit Generators | ||
-------------- | ||
|
||
.. currentmodule:: numpy.random | ||
|
||
The random values produced by :class:`~Generator` | ||
orignate in a BitGenerator. The BitGenerators do not directly provide | ||
random numbers and only contains methods used for seeding, getting or | ||
setting the state, jumping or advancing the state, and for accessing | ||
low-level wrappers for consumption by code that can efficiently | ||
access the functions provided, e.g., `numba <https://numba.pydata.org>`_. | ||
|
||
Stable RNGs | ||
=========== | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
BitGenerator <bitgenerators> | ||
MT19937 <mt19937> | ||
PCG64 <pcg64> | ||
Philox <philox> | ||
|
||
Seeding and Entropy | ||
------------------- | ||
|
||
A BitGenerator provides a stream of random values. In order to generate | ||
reproducableis streams, BitGenerators support setting their initial state via a | ||
seed. But how best to seed the BitGenerator? On first impulse one would like to | ||
do something like ``[bg(i) for i in range(12)]`` to obtain 12 non-correlated, | ||
independent BitGenerators. However using a highly correlated set of seeds could | ||
generate BitGenerators that are correlated or overlap within a few samples. | ||
|
||
NumPy uses a `SeedSequence` class to mix the seed in a reproducible way that | ||
introduces the necessary entropy to produce independent and largely non- | ||
overlapping streams. Small seeds may still be unable to reach all possible | ||
initialization states, which can cause biases among an ensemble of small-seed | ||
runs. For many cases, that doesn't matter. If you just want to hold things in | ||
place while you debug something, biases aren't a concern. For actual | ||
simulations whose results you care about, let ``SeedSequence(None)`` do its | ||
thing and then log/print the `SeedSequence.entropy` for repeatable | ||
`BitGenerator` streams. | ||
|
||
.. autosummary:: | ||
:toctree: generated/ | ||
|
||
bit_generator.ISeedSequence | ||
bit_generator.ISpawnableSeedSequence | ||
SeedSequence | ||
bit_generator.SeedlessSeedSequence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
from .common cimport bitgen_t | ||
cimport numpy as np | ||
|
||
cdef class BitGenerator(): | ||
cdef readonly object _seed_seq | ||
cdef readonly object lock | ||
cdef bitgen_t _bitgen | ||
cdef readonly object _ctypes | ||
cdef readonly object _cffi | ||
cdef readonly object capsule | ||
|
||
|
||
cdef class SeedSequence(): | ||
cdef readonly object entropy | ||
cdef readonly object program_entropy | ||
cdef readonly tuple spawn_key | ||
cdef readonly int pool_size | ||
cdef readonly object pool | ||
cdef readonly int n_children_spawned | ||
|
||
cdef mix_entropy(self, np.ndarray[np.npy_uint32, ndim=1] mixer, | ||
np.ndarray[np.npy_uint32, ndim=1] entropy_array) | ||
cdef get_assembled_entropy(self) | ||
|
||
cdef class SeedlessSequence(): | ||
pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.