1
1
.. _numpyrandom :
2
2
3
+ .. py :module :: numpy.random
4
+
3
5
.. currentmodule :: numpy.random
4
6
5
- numpy.random
6
- ============
7
+ Random sampling ( :mod: ` numpy.random `)
8
+ =====================================
7
9
8
10
Numpy's random number routines produce pseudo random numbers using
9
11
combinations of a `BitGenerator ` to create sequences and a `Generator `
10
12
to use those sequences to sample from different statistical distributions:
11
13
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
14
* BitGenerators: Objects that generate random numbers. These are typically
17
15
unsigned integer words filled with sequences of either 32 or 64 random bits.
18
16
* Generators: Objects that transform sequences of random bits from a
@@ -24,28 +22,28 @@ Since Numpy version 1.17.0 the Generator can be initialized with a
24
22
number of different BitGenerators. It exposes many different probability
25
23
distributions. See `NEP 19 <https://www.numpy.org/neps/
26
24
nep-0019-rng-policy.html> `_ for context on the updated random Numpy number
27
- routines. The legacy `RandomState ` random number routines are still
25
+ routines. The legacy `. RandomState ` random number routines are still
28
26
available, but limited to a single BitGenerator.
29
27
30
- For convenience and backward compatibility, a single `RandomState `
28
+ For convenience and backward compatibility, a single `~. RandomState `
31
29
instance's methods are imported into the numpy.random namespace, see
32
30
:ref: `legacy ` for the complete list.
33
31
34
32
Quick Start
35
33
-----------
36
34
37
- By default, `Generator ` uses normals provided by `PCG64 ` which will be
38
- statistically more reliable than the legacy methods in `RandomState `
35
+ By default, `~ Generator ` uses normals provided by `~pcg64. PCG64 ` which will be
36
+ statistically more reliable than the legacy methods in `~. RandomState `
39
37
40
38
.. code-block :: python
41
39
42
40
# Uses the old numpy.random.RandomState
43
41
from numpy import random
44
42
random.standard_normal()
45
43
46
- `Generator ` can be used as a direct replacement for `~RandomState `, although
47
- the random values are generated by `~PCG64 `. The
48
- `Generator ` holds an instance of a BitGenerator. It is accessible as
44
+ `~ Generator ` can be used as a direct replacement for `~. RandomState `, although
45
+ the random values are generated by `~. PCG64 `. The
46
+ `~ Generator ` holds an instance of a BitGenerator. It is accessible as
49
47
``gen.bit_generator ``.
50
48
51
49
.. code-block :: python
@@ -69,45 +67,37 @@ is wrapped with a `~.Generator`.
69
67
70
68
Introduction
71
69
------------
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.
70
+ The new infrastructure takes a different approach to producing random numbers
71
+ from the ` ~. RandomState ` object. Random number generation is separated into
72
+ two components , a bit generator and a random generator.
75
73
76
74
The `BitGenerator ` has a limited set of responsibilities. It manages state
77
75
and provides functions to produce random doubles and random unsigned 32- and
78
76
64-bit values.
79
77
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
78
The `random generator <Generator> ` takes the
92
79
bit generator-provided stream and transforms them into more useful
93
80
distributions, e.g., simulated normal random values. This structure allows
94
81
alternative bit generators to be used with little code duplication.
95
82
96
83
The `Generator ` is the user-facing object that is nearly identical to
97
- `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.
84
+ `. RandomState `. The canonical method to initialize a generator passes a
85
+ `~.PCG64 ` bit generator as the sole argument.
86
+
100
87
.. code-block :: python
101
88
102
- from numpy.random import Generator, PCG64
103
- rg = Generator(PCG64() )
89
+ from numpy.random import default_gen
90
+ rg = default_gen( 12345 )
104
91
rg.random()
105
92
106
- Seed information is directly passed to the bit generator.
93
+ One can also instantiate `Generator ` directly with a `BitGenerator ` instance.
94
+ To use the older `~mt19937.MT19937 ` algorithm, one can instantiate it directly
95
+ and pass it to `Generator `.
107
96
108
97
.. code-block :: python
109
98
110
- rg = Generator(PCG64(12345 ))
99
+ from numpy.random import Generator, MT19937
100
+
1241
rg = Generator(MT19937(12345 ))
111
101
rg.random()
112
102
113
103
What's New or Different
F438
@@ -117,9 +107,9 @@ What's New or Different
117
107
The Box-Muller method used to produce NumPy's normals is no longer available
118
108
in `Generator `. It is not possible to reproduce the exact random
119
109
values using Generator for the normal distribution or any other
120
- distribution that relies on the normal such as the `numpy.random .gamma ` or
121
- `numpy.random .standard_t `. If you require bitwise backward compatible
122
- streams, use `RandomState `.
110
+ distribution that relies on the normal such as the `.RandomState .gamma ` or
111
+ `.RandomState .standard_t `. If you require bitwise backward compatible
112
+ streams, use `. RandomState `.
123
113
124
114
* The Generator's normal, exponential and gamma functions use 256-step Ziggurat
125
115
methods which are 2-10 times faster than NumPy's Box-Muller or inverse CDF
@@ -133,9 +123,8 @@ What's New or Different
133
123
source of randomness that is used in cryptographic applications (e.g.,
134
124
``/dev/urandom `` on Unix).
135
125
* All BitGenerators can produce doubles, uint64s and uint32s via CTypes
136
- (`~PCG64.ctypes `) and CFFI
137
- (:meth: `~PCG64.cffi `). This allows the bit generators to
138
- be used in numba.
126
+ (`~.PCG64.ctypes `) and CFFI (`~.PCG64.cffi `). This allows the bit generators
127
+ to be used in numba.
139
128
* The bit generators can be used in downstream projects via
140
129
:ref: `Cython <randomgen_cython >`.
141
130
* `~.Generator.integers ` is now the canonical way to generate integer
@@ -144,8 +133,11 @@ What's New or Different
144
133
The ``endpoint `` keyword can be used to specify open or closed intervals.
145
134
This replaces both ``randint `` and the deprecated ``random_integers ``.
146
135
* `~.Generator.random ` is now the canonical way to generate floating-point
147
- random numbers, which replaces `random_sample `, `sample `, and `ranf `. This
148
- is consistent with Python's `random.random `.
136
+ random numbers, which replaces `.RandomState.random_sample `,
137
+ `.RandomState.sample `, and `.RandomState.ranf `. This is consistent with
138
+ Python's `random.random `.
139
+ * All BitGenerators in numpy use `~SeedSequence ` to convert seeds into
140
+ initialized states.
149
141
150
142
See :ref: `new-or-different ` for a complete list of improvements and
151
143
differences from the traditional ``Randomstate ``.
@@ -154,10 +146,11 @@ Parallel Generation
154
146
~~~~~~~~~~~~~~~~~~~
155
147
156
148
The included generators can be used in parallel, distributed applications in
157
- one of two ways:
149
+ one of three ways:
158
150
151
+ * :ref: `seedsequence-spawn `
159
152
* :ref: `independent-streams `
160
- * :ref: `jump-and-advance `
153
+ * :ref: `parallel-jumped `
161
154
162
155
Concepts
163
156
--------
0 commit comments