Skip to content

Commit

Permalink
MAINT: Update typing information
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtage committed Jul 15, 2022
1 parent 8147a66 commit c5c79cd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions doc/release/upcoming_changes/21976.new_feature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ The function ``set_bit_generator`` allows the default bit generator to be
replaced with a user-provided bit generator. This function has been introduced to
provide a method allowing seemless integration of a high-quality, modern bit generator
in new code with existing code that makes use of the singleton-provided random
variate generating functions.
variate generating functions. The companion function ``get_bit_generator`` returns the current bit generator
being used by the singleton ``RandomState``. This is provided to simplify restoring
the original source of randomness if required.

The preferred method to generate reproducible random numbers is to use a modern
bit generator in an instance of ``Generator``. The function ``default_rng``
Expand All @@ -18,8 +20,12 @@ simplifies instantization.
The same bit generator can then shared with the singleton instance so that
calling functions in the ``random`` module will use the same bit generator.

>>> orig_bit_gen = np.random.get_bit_generator()
>>> np.random.set_bit_generator(rg.bit_generator)
>>> np.random.normal()

The swap is permanent (until reversed) and so any call to functions
in the ``random`` module will use the new bit generator.
in the ``random`` module will use the new bit generator. The orginal
can be restored if required for code to run correctly.

>>> np.random.set_bit_generator(orig_bit_gen)
6 changes: 6 additions & 0 deletions numpy/random/mtrand.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,9 @@ zipf = _rand.zipf
# Two legacy that are trivial wrappers around random_sample
sample = _rand.random_sample
ranf = _rand.random_sample

def set_bit_generator(bitgen: BitGenerator) -> None:
...

def get_bit_generator() -> BitGenerator:
...
3 changes: 3 additions & 0 deletions numpy/typing/tests/data/pass/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1494,3 +1494,6 @@
random_st.tomaxint()
random_st.tomaxint(1)
random_st.tomaxint((1,))

np.random.set_bit_generator(SEED_PCG64)
np.random.get_bit_generator()
3 changes: 3 additions & 0 deletions numpy/typing/tests/data/reveal/random.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1537,3 +1537,6 @@ reveal_type(random_st.random_sample(size=(1, 2))) # E: ndarray[Any, dtype[float
reveal_type(random_st.tomaxint()) # E: int
reveal_type(random_st.tomaxint(1)) # E: ndarray[Any, dtype[{int_}]]
reveal_type(random_st.tomaxint((1,))) # E: ndarray[Any, dtype[{int_}]]

reveal_type(np.random.set_bit_generator(pcg64)) # E: BitGenerator
reveal_type(np.random.get_bit_generator()) # E: None

0 comments on commit c5c79cd

Please sign in to comment.