@@ -22,34 +22,44 @@ Since Numpy version 1.17.0 the Generator can be initialized with a
22
22
number of different BitGenerators. It exposes many different probability
23
23
distributions. See `NEP 19 <https://www.numpy.org/neps/
24
24
nep-0019-rng-policy.html> `_ for context on the updated random Numpy number
25
- routines. The legacy `. RandomState ` random number routines are still
25
+ routines. The legacy `RandomState ` random number routines are still
26
26
available, but limited to a single BitGenerator.
27
27
28
- For convenience and backward compatibility, a single `~. RandomState `
28
+ For convenience and backward compatibility, a single `RandomState `
29
29
instance's methods are imported into the numpy.random namespace, see
30
30
:ref: `legacy ` for the complete list.
31
31
32
+ .. _random-quick-start :
33
+
32
34
Quick Start
33
35
-----------
34
36
35
- By default, `~Generator ` uses bits provided by `PCG64 ` which
36
- has better statistical properties than the legacy mt19937 random
37
- number generator in `~.RandomState `.
37
+ Call `default_rng ` to get a new instance of a `Generator `, then call its
38
+ methods to obtain samples from different distributions. By default,
39
+ `Generator ` uses bits provided by `PCG64 ` which has better statistical
40
+ properties than the legacy `MT19937 ` used in `RandomState `.
38
41
39
42
.. code-block :: python
40
43
41
- # Uses the old numpy.random.RandomState
44
+ # Do this
45
+ from numpy.random import default_rng
46
+ rng = default_rng()
47
+ vals = rng.standard_normal(10 )
48
+ more_vals = rng.standard_normal(10 )
49
+
50
+ # instead of this
42
51
from numpy import random
43
- random.standard_normal()
52
+ vals = random.standard_normal(10 )
53
+ more_vals = random.standard_normal(10 )
44
54
45
- `~ Generator ` can be used as a replacement for `~. RandomState `. Both class
46
- instances now hold a internal `BitGenerator ` instance to provide the bit
55
+ `Generator ` can be used as a replacement for `RandomState `. Both class
56
+ instances hold a internal `BitGenerator ` instance to provide the bit
47
57
stream, it is accessible as ``gen.bit_generator ``. Some long-overdue API
48
58
cleanup means that legacy and compatibility methods have been removed from
49
- `~. Generator `
59
+ `Generator `
50
60
51
61
=================== ============== ============
52
- `~. RandomState ` ` ~. Generator ` Notes
62
+ `RandomState ` ` Generator ` Notes
53
63
------------------- -------------- ------------
54
64
``random_sample ``, ``random `` Compatible with `random.random `
55
65
``rand ``
@@ -58,21 +68,12 @@ cleanup means that legacy and compatibility methods have been removed from
58
68
``random_integers ``
59
69
------------------- -------------- ------------
60
70
``tomaxint `` removed Use ``integers(0, np.iinfo(np.int_).max, ``
61
- ``endpoint=False) ``
71
+ ``endpoint=False) ``
62
72
------------------- -------------- ------------
63
- ``seed `` removed Use `~. SeedSequence.spawn `
73
+ ``seed `` removed Use `SeedSequence.spawn `
64
74
=================== ============== ============
65
75
66
- See `new-or-different ` for more information
67
-
68
- .. code-block :: python
69
-
70
- # As replacement for RandomState(); default_rng() instantiates Generator with
71
- # the default PCG64 BitGenerator.
72
- from numpy.random import default_rng
73
- rg = default_rng()
74
- rg.standard_normal()
75
- rg.bit_generator
76
+ See :ref: `new-or-different ` for more information
76
77
77
78
Something like the following code can be used to support both ``RandomState ``
78
79
and ``Generator ``, with the understanding that the interfaces are slightly
@@ -87,9 +88,9 @@ different
87
88
a = rg_integers(1000 )
88
89
89
90
Seeds can be passed to any of the BitGenerators. The provided value is mixed
90
- via `~. SeedSequence ` to spread a possible sequence of seeds across a wider
91
- range of initialization states for the BitGenerator. Here `~. PCG64 ` is used and
92
- is wrapped with a `~. Generator `.
91
+ via `SeedSequence ` to spread a possible sequence of seeds across a wider
92
+ range of initialization states for the BitGenerator. Here `PCG64 ` is used and
93
+ is wrapped with a `Generator `.
93
94
94
95
.. code-block :: python
95
96
@@ -100,7 +101,7 @@ is wrapped with a `~.Generator`.
100
101
Introduction
101
102
------------
102
103
The new infrastructure takes a different approach to producing random numbers
103
- from the `~. RandomState ` object. Random number generation is separated into
104
+ from the `RandomState ` object. Random number generation is separated into
104
105
two components, a bit generator and a random generator.
105
106
106
107
The `BitGenerator ` has a limited set of responsibilities. It manages state
@@ -113,8 +114,8 @@ distributions, e.g., simulated normal random values. This structure allows
113
114
alternative bit generators to be used with little code duplication.
114
115
115
116
The `Generator ` is the user-facing object that is nearly identical to
116
- `. RandomState `. The canonical method to initialize a generator passes a
117
- `~. PCG64 ` bit generator as the sole argument.
117
+ `RandomState `. The canonical method to initialize a generator passes a
118
+ `PCG64 ` bit generator as the sole argument.
118
119
119
120
.. code-block :: python
120
121
@@ -139,9 +140,9 @@ What's New or Different
139
140
The Box-Muller method used to produce NumPy's normals is no longer available
140
141
in `Generator `. It is not possible to reproduce the exact random
141
142
values using Generator for the normal distribution or any other
142
- distribution that relies on the normal such as the `. RandomState.gamma ` or
143
- `. RandomState.standard_t `. If you require bitwise backward compatible
144
- streams, use `. RandomState `.
143
+ distribution that relies on the normal such as the `RandomState.gamma ` or
144
+ `RandomState.standard_t `. If you require bitwise backward compatible
145
+ streams, use `RandomState `.
145
146
146
147
* The Generator's normal, exponential and gamma functions use 256-step Ziggurat
147
148
methods which are 2-10 times faster than NumPy's Box-Muller or inverse CDF
@@ -152,20 +153,20 @@ What's New or Different
152
153
* Optional ``out `` argument that allows existing arrays to be filled for
153
154
select distributions
154
155
* All BitGenerators can produce doubles, uint64s and uint32s via CTypes
155
- (`~. PCG64.ctypes `) and CFFI (`~. PCG64.cffi `). This allows the bit generators
156
+ (`PCG64.ctypes `) and CFFI (`PCG64.cffi `). This allows the bit generators
156
157
to be used in numba.
157
158
* The bit generators can be used in downstream projects via
158
159
:ref: `Cython <random_cython >`.
159
- * `~. Generator.integers ` is now the canonical way to generate integer
160
+ * `Generator.integers ` is now the canonical way to generate integer
160
161
random numbers from a discrete uniform distribution. The ``rand `` and
161
- ``randn `` methods are only available through the legacy `~. RandomState `.
162
+ ``randn `` methods are only available through the legacy `RandomState `.
162
163
The ``endpoint `` keyword can be used to specify open or closed intervals.
163
164
This replaces both ``randint `` and the deprecated ``random_integers ``.
164
- * `~. Generator.random ` is now the canonical way to generate floating-point
165
- random numbers, which replaces `. RandomState.random_sample `,
166
- `. RandomState.sample `, and `. RandomState.ranf `. This is consistent with
165
+ * `Generator.random ` is now the canonical way to generate floating-point
166
+ random numbers, which replaces `RandomState.random_sample `,
167
+ `RandomState.sample `, and `RandomState.ranf `. This is consistent with
167
168
Python's `random.random `.
168
- * All BitGenerators in numpy use `~ SeedSequence ` to convert seeds into
169
+ * All BitGenerators in numpy use `SeedSequence ` to convert seeds into
169
170
initialized states.
170
171
171
172
See :ref: `new-or-different ` for a complete list of improvements and
@@ -202,8 +203,9 @@ Features
202
203
c-api
203
204
Examples of using Numba, Cython, CFFI <extending >
204
205
205
- Original Source
206
- ~~~~~~~~~~~~~~~
206
+ Original Source of the Generator and BitGenerators
207
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207
208
208
209
This package was developed independently of NumPy and was integrated in version
209
210
1.17.0. The original repo is at https://github.com/bashtage/randomgen.
211
+
0 commit comments