Skip to content

Commit 8c28a72

Browse files
authored
DOC: SPEC 7: update for NumPy 2.2.0 (#365)
numpy/numpy#27771 allows `default_rng` to accept instances of `RandomState` and convert them to a `Generator`. This updates the SPEC accordingly.
1 parent 79fbd99 commit 8c28a72

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*~
22
\#*
33
**/__pycache__
4+
.idea

spec-0007/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ We are primarily concerned with API uniformity, but also encourage libraries to
3838
2. their use avoids relying on global state—which can make code execution harder to track, and may cause problems in parallel processing scenarios.
3939

4040
[^no-RandomState]:
41-
Note that `numpy.random.default_rng` does not accept instances of `RandomState`, so use of `RandomState` to control the seed is effectively deprecated, too.
42-
That said, neither `np.random.seed` nor `np.random.RandomState` _themselves_ are deprecated, so they may still be used in some contexts (e.g. by developers for generating unit test data).
41+
Note that in NumPy versions prior to 2.2.0, `numpy.random.default_rng` does not accept instances of `RandomState`.
42+
In more recent versions, `numpy.random.default_rng` will convert `RandomState` instances to `Generator`s, which may not behave identically even with identical method calls.
43+
That said, neither `np.random.seed` nor `np.random.RandomState` _themselves_ are deprecated, so they may still be used directly in some contexts (e.g. by developers for generating unit test data).
4344

4445
### Scope
4546

4647
This is intended as a recommendation to all libraries that allow users to control the state of a NumPy random number generator.
47-
It is specifically targeted toward functions that currently accept `RandomState` instances via an argument other than `rng`, or allow `numpy.random.seed` to control the random state, but the ideas are more broadly applicable.
48+
It is specifically targeted toward functions that currently accept random number seeds using an argument other than `rng`, rely on the particular behavior of `RandomState` methods, or allow `numpy.random.seed` to control the random state, but the ideas are more broadly applicable.
4849
Random number generators other than those provided by NumPy could also be accommodated by an `rng` keyword, but that is beyond the scope of this SPEC.
4950

5051
### Concepts
@@ -124,7 +125,7 @@ def my_func(*, rng: RNGLike | SeedLike | None = None):
124125
Pseudorandom number generator state. When `rng` is None, a new
125126
`numpy.random.Generator` is created using entropy from the
126127
operating system. Types other than `numpy.random.Generator` are
127-
passed to `numpy.random.default_rng` to instantiate a `Generator`.
128+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
128129
"""
129130
rng = np.random.default_rng(rng)
130131

spec-0007/transition_to_rng.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ def _transition_to_rng(old_name, *, position_num=None, end_version=None):
4343
- If `None` is passed by position and `np.random.seed` has been used,
4444
the function will change from being seeded to being unseeded.
4545
- If an integer is passed by position, the random stream will change.
46-
- If `np.random` or an instance of `RandomState` is passed by position,
47-
an error will be raised.
46+
- If an instance of `RandomState` is passed by position, either an
47+
error will be raised (NumPy < 2.2.0), or it will be accepted
48+
(NumPy >= 2.2.0) but the random numbers generated may be different
49+
from those that would be produced by the original `RandomState`.
50+
- If `np.random` is passed by position, an error will be raised.
4851
4952
We suggest that projects consider deprecating positional use of
5053
`random_state`/`rng` (i.e., change their function signatures to

0 commit comments

Comments
 (0)