bundles / numpy 2.4.4 / docs
Doc
reference:random:new-or-different
docs/reference:random:new-or-different
What's new or different
NumPy 1.17.0 introduced Generator as an improved replacement for the legacy RandomState. Here is a quick comparison of the two implementations.
======================= ================== ============= Feature Older Equivalent Notes ----------------------- ------------------ ------------- `Generator` `RandomState` `Generator` requires a stream source, called a `BitGenerator` A number of these are provided. `RandomState` uses the Mersenne Twister `MT19937` by default, but can also be instantiated with any BitGenerator. ----------------------- ------------------ -------------
~.Generator.randomrandom_sample, Access the values in arandBitGenerator, convert them tofloat64in the interval[0.0., 1.0). In addition to thesizekwarg, now supportsdtype='d'ordtype='f', and anoutkwarg to fill a user-supplied array.Many other distributions are also supported.
----------------------- ------------------ -------------
~.Generator.integersrandint, Use theendpointkwarg torandom_integersadjust the inclusion or exclusionof the
highinterval endpoint.
======================= ================== ============= * The normal, exponential and gamma generators use 256-step Ziggurat
methods which are 2-10 times faster than NumPy's default implementation in
~.Generator.standard_normal,~.Generator.standard_exponentialor~.Generator.standard_gamma. Because of the change in algorithms, it is not possible to reproduce the exact random values usingGeneratorfor these distributions or any distribution method that relies on them.
~.Generator.integersis now the canonical way to generate integer random numbers from a discrete uniform distribution. This replaces bothrandintand the deprecatedrandom_integers.The
randandrandnmethods are only available through the legacy~.RandomState.Generator.randomis now the canonical way to generate floating-point random numbers, which replacesRandomState.random_sample,sample, andranf, all of which were aliases. This is consistent with Python'srandom.random.All bit generators can produce doubles, uint64s and uint32s via CTypes (
~PCG64.ctypes) and CFFI (~PCG64.cffi). This allows these bit generators to be used in numba.The bit generators can be used in downstream projects via Cython.
All bit generators use
SeedSequenceto convert seed integers to initialized states.Optional
dtypeargument that acceptsnp.float32ornp.float64to produce either single or double precision uniform random variables for select distributions.~.Generator.integersaccepts adtypeargument with any signed or unsigned integer dtype.Uniforms (
~.Generator.randomand~.Generator.integers)Normals (
~.Generator.standard_normal)Standard Gammas (
~.Generator.standard_gamma)Standard Exponentials (
~.Generator.standard_exponential)
Optional
outargument that allows existing arrays to be filled for select distributionsUniforms (
~.Generator.random)Normals (
~.Generator.standard_normal)Standard Gammas (
~.Generator.standard_gamma)Standard Exponentials (
~.Generator.standard_exponential)
This allows multithreading to fill large arrays in chunks using suitable BitGenerators in parallel.
Optional
axisargument for methods like~.Generator.choice,~.Generator.permutationand~.Generator.shufflethat controls which axis an operation is performed over for multi-dimensional arrays.
Added a method to sample from the complex normal distribution (
~.Generator.complex_normal)