@@ -9,10 +9,6 @@ Numpy's random number routines produce pseudo random numbers using
9
9
combinations of a `BitGenerator ` to create sequences and a `Generator `
10
10
to use those sequences to sample from different statistical distributions:
11
11
12
- * SeedSequence: Objects that provide entropy for the initial state of a
13
- BitGenerator. A good SeedSequence will provide initializations across the
14
- entire range of possible states for the BitGenerator, otherwise biases may
15
- creep into the generated bit streams.
16
12
* BitGenerators: Objects that generate random numbers. These are typically
17
13
unsigned integer words filled with sequences of either 32 or 64 random bits.
18
14
* Generators: Objects that transform sequences of random bits from a
@@ -24,28 +20,28 @@ Since Numpy version 1.17.0 the Generator can be initialized with a
24
20
number of different BitGenerators. It exposes many different probability
25
21
distributions. See `NEP 19 <https://www.numpy.org/neps/
26
22
nep-0019-rng-policy.html> `_ for context on the updated random Numpy number
27
- routines. The legacy `RandomState ` random number routines are still
23
+ routines. The legacy `~ RandomState ` random number routines are still
28
24
available, but limited to a single BitGenerator.
29
25
30
- For convenience and backward compatibility, a single `RandomState `
26
+ For convenience and backward compatibility, a single `~ RandomState `
31
27
instance's methods are imported into the numpy.random namespace, see
32
28
:ref: `legacy ` for the complete list.
33
29
34
30
Quick Start
35
31
-----------
36
32
37
- By default, `Generator ` uses normals provided by `PCG64 ` which will be
38
- statistically more reliable than the legacy methods in `RandomState `
33
+ By default, `~ Generator ` uses normals provided by `~ PCG64 ` which will be
34
+ statistically more reliable than the legacy methods in `~ RandomState `
39
35
40
36
.. code-block :: python
41
37
42
38
# Uses the old numpy.random.RandomState
43
39
from numpy import random
44
40
random.standard_normal()
45
41
46
- `Generator ` can be used as a direct replacement for `~RandomState `, although
42
+ `~ Generator ` can be used as a direct replacement for `~RandomState `, although
47
43
the random values are generated by `~PCG64 `. The
48
- `Generator ` holds an instance of a BitGenerator. It is accessible as
44
+ `~ Generator ` holds an instance of a BitGenerator. It is accessible as
49
45
``gen.bit_generator ``.
50
46
51
47
.. code-block :: python
@@ -69,45 +65,37 @@ is wrapped with a `~.Generator`.
69
65
70
66
Introduction
71
67
------------
72
- RandomGen takes a different approach to producing random numbers from the
73
- `RandomState ` object. Random number generation is separated into three
74
- components, a seed sequence , a bit generator and a random generator.
68
+ The new infrastructure takes a different approach to producing random numbers
69
+ from the `RandomState ` object. Random number generation is separated into
70
+ two components , a bit generator and a random generator.
75
71
76
72
The `BitGenerator ` has a limited set of responsibilities. It manages state
77
73
and provides functions to produce random doubles and random unsigned 32- and
78
74
64-bit values.
79
75
80
- The `SeedSequence ` takes a seed and provides the initial state for the
81
- `BitGenerator `. Since consecutive seeds can cause bad effects when comparing
82
- `BitGenerator ` streams, the `SeedSequence ` uses current best-practice methods
83
- to spread the initial state out. However small seeds may still be unable to
84
- reach all possible initialization states, which can cause biases among an
85
- ensemble of small-seed runs. For many cases, that doesn't matter. If you just
86
- want to hold things in place while you debug something, biases aren't a
87
- concern. For actual simulations whose results you care about, let
88
- ``SeedSequence(None) `` do its thing and then log/print the
89
- `SeedSequence.entropy ` for repeatable `BitGenerator ` streams.
90
-
91
76
The `random generator <Generator> ` takes the
92
77
bit generator-provided stream and transforms them into more useful
93
78
distributions, e.g., simulated normal random values. This structure allows
94
79
alternative bit generators to be used with little code duplication.
95
80
96
81
The `Generator ` is the user-facing object that is nearly identical to
97
82
`RandomState `. The canonical method to initialize a generator passes a
98
- `~mt19937.MT19937 ` bit generator, the underlying bit generator in Python -- as
99
- the sole argument. Note that the BitGenerator must be instantiated.
83
+ `~pcg64.PCG64 ` bit generator as
1C6A
the sole argument.
84
+
100
85
.. code-block :: python
101
86
102
- from numpy.random import Generator, PCG64
103
- rg = Generator(PCG64() )
87
+ from numpy.random import default_gen
88
+ rg = default_gen( 12345 )
104
89
rg.random()
105
90
106
- Seed information is directly passed to the bit generator.
91
+ One can also instantiate `Generator ` directly with a `BitGenerator ` instance.
92
+ To use the older `~mt19937.MT19937 ` algorithm, one can instantiate it directly
93
+ and pass it to `Generator `.
107
94
108
95
.. code-block :: python
109
96
110
- rg = Generator(PCG64(12345 ))
97
+ from numpy.random import Generator, MT19937
98
+ rg = Generator(MT19937(12345 ))
111
99
rg.random()
112
100
113
101
What's New or Different
@@ -146,6 +134,8 @@ What's New or Different
146
134
* `~.Generator.random ` is now the canonical way to generate floating-point
147
135
random numbers, which replaces `random_sample `, `sample `, and `ranf `. This
148
136
is consistent with Python's `random.random `.
137
+ * All BitGenerators in numpy use `~SeedSequence ` to process convert seeds into
138
+ initialized states.
149
139
150
140
See :ref: `new-or-different ` for a complete list of improvements and
151
141
differences from the traditional ``Randomstate ``.
@@ -154,10 +144,11 @@ Parallel Generation
154
144
~~~~~~~~~~~~~~~~~~~
155
145
156
146
The included generators can be used in parallel, distributed applications in
157
- one of two ways:
147
+ one of three ways:
158
148
149
+ * :ref: `seedsequence-spawn `
159
150
* :ref: `independent-streams `
160
- * :ref: `jump-and-advance `
151
+ * :ref: `parallel-jumped `
161
152
162
153
Concepts
163
154
--------
0 commit comments