1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
.. currentmodule:: numpy.random
.. _legacy:
Legacy Random Generation
------------------------
The `RandomState` provides access to
legacy generators. This generator is considered frozen and will have
no further improvements. It is guaranteed to produce the same values
as the final point release of NumPy v1.16. These all depend on Box-Muller
normals or inverse CDF exponentials or gammas. This class should only be used
if it is essential to have randoms that are identical to what
would have been produced by previous versions of NumPy.
`RandomState` adds additional information
to the state which is required when using Box-Muller normals since these
are produced in pairs. It is important to use
`RandomState.get_state`, and not the underlying bit generators
`state`, when accessing the state so that these extra values are saved.
Although we provide the `MT19937` BitGenerator for use independent of
`RandomState`, note that its default seeding uses `SeedSequence`
rather than the legacy seeding algorithm. `RandomState` will use the
legacy seeding algorithm. The methods to use the legacy seeding algorithm are
currently private as the main reason to use them is just to implement
`RandomState`. However, one can reset the state of `MT19937`
using the state of the `RandomState`:
.. code-block:: python
from numpy.random import MT19937
from numpy.random import RandomState
rs = RandomState(12345)
mt19937 = MT19937()
mt19937.state = rs.get_state()
rs2 = RandomState(mt19937)
# Same output
rs.standard_normal()
rs2.standard_normal()
rs.random()
rs2.random()
rs.standard_exponential()
rs2.standard_exponential()
.. autoclass:: RandomState
:members: __init__
:exclude-members: __init__
Seeding and State
-----------------
.. autosummary::
:toctree: generated/
~RandomState.get_state
~RandomState.set_state
~RandomState.seed
Simple random data
------------------
.. autosummary::
:toctree: generated/
~RandomState.rand
~RandomState.randn
~RandomState.randint
~RandomState.random_integers
~RandomState.random_sample
~RandomState.choice
~RandomState.bytes
Permutations
------------
.. autosummary::
:toctree: generated/
~RandomState.shuffle
~RandomState.permutation
Distributions
-------------
.. autosummary::
:toctree: generated/
~RandomState.beta
~RandomState.binomial
~RandomState.chisquare
~RandomState.dirichlet
~RandomState.exponential
~RandomState.f
~RandomState.gamma
~RandomState.geometric
~RandomState.gumbel
~RandomState.hypergeometric
~RandomState.laplace
~RandomState.logistic
~RandomState.lognormal
~RandomState.logseries
~RandomState.multinomial
~RandomState.multivariate_normal
~RandomState.negative_binomial
~RandomState.noncentral_chisquare
~RandomState.noncentral_f
~RandomState.normal
~RandomState.pareto
~RandomState.poisson
~RandomState.power
~RandomState.rayleigh
~RandomState.standard_cauchy
~RandomState.standard_exponential
~RandomState.standard_gamma
~RandomState.standard_normal
~RandomState.standard_t
~RandomState.triangular
~RandomState.uniform
~RandomState.vonmises
~RandomState.wald
~RandomState.weibull
~RandomState.zipf
Functions in `numpy.random`
---------------------------
Many of the RandomState methods above are exported as functions in
`numpy.random` This usage is discouraged, as it is implemented via a global
`RandomState` instance which is not advised on two counts:
- It uses global state, which means results will change as the code changes
- It uses a `RandomState` rather than the more modern `Generator`.
For backward compatible legacy reasons, we cannot change this. See
:ref:`random-quick-start`.
.. autosummary::
:toctree: generated/
beta
binomial
bytes
chisquare
choice
dirichlet
exponential
f
gamma
geometric
get_state
gumbel
hypergeometric
laplace
logistic
lognormal
logseries
multinomial
multivariate_normal
negative_binomial
noncentral_chisquare
noncentral_f
normal
pareto
permutation
poisson
power
rand
randint
randn
random
random_integers
random_sample
ranf
rayleigh
sample
seed
set_state
shuffle
standard_cauchy
standard_exponential
standard_gamma
standard_normal
standard_t
triangular
uniform
vonmises
wald
weibull
zipf
|