Random sampling (dpnp.random)¶
DPNP draws random numbers on the SYCL device, by default with the
counter-based Philox4x32x10 generator of the oneMKL device RNG API, or with
NumPy's PCG64DXSM generator or the MRG32k3a generator of the oneMKL device
RNG API. It provides both NumPy random APIs: the Generator API recommended for new code,
and the legacy RandomState API with the module-level functions
built on it.
import dpnp
rng = dpnp.random.default_rng(seed=42)
x = rng.standard_normal((3, 4)) # on the default device
y = rng.integers(0, 10, size=5, usm_type="shared")
rng = dpnp.random.default_rng(42, device="cpu")
A generator draws all its samples on one SYCL queue, chosen with the
device or sycl_queue keyword when it is created, and the samples
depend only on the seed, not on the device, except that the discrete
distributions also depend on its support of double precision. All samples are
dpnp.ndarray, zero-dimensional when size is None.
Differences from NumPy¶
The same seed gives the same stream on every device, but for the discrete distributions noted below. The streams differ from NumPy's, except that
PCG64DXSMgives NumPy's raw values,Generator.random(),Generator.uniform()andGenerator.bytes(), and can exchange states withnumpy.random.PCG64DXSM.The floating-point distributions take a
dtypekeyword, as in CuPy, which defaults to the default floating-point type of the device. All methods take ausm_typekeyword.Parameters on the host (Python scalars, lists and NumPy arrays) are checked as in NumPy. In
Generator, parameters indpnp.ndarrayare not, since that would wait for the device. Invalid ones give undefined values, such as NaN or the smallestint64, but the kernels handle them safely: they never loop without end or access memory out of bounds. Floating-point arrays given for integer parameters are converted as bydpnp.ndarray.astype(), whose result for NaN and out-of-range values is undefined.The discrete distributions return
int64values. They compute in double precision on devices supporting it, and in single precision otherwise. There, the streams differ from the ones of devices with double precision, and some samples are only multiples of a power of two: the ones above2**24ofgeometric,logseriesandzipf, and the ones ofpoissonandbinomialwith a standard deviation above about2**22.zipffollows the Zipf distribution truncated to the largestint64. NumPy's rejection test rounds to accepting every sample above2**53, which makes its tail too heavy foraclose to 1.RandomState.randint()andrandint()returnint64values by default, as doRandomState.random_integers(),random_integers()andRandomState.tomaxint(), whose values reach the largestint64. NumPy returns a Clonginstead, which isint32on Windows.hypergeometricrequiresngoodandnbadbelow10**9, also inRandomState.Generator.shuffle()andshuffle()only accept adpnp.ndarray, as in CuPy.
Random Generator¶
Construct a new |
|
A container for the bit generator and the distribution samplers. |
Simple random data¶
Return random integers from low (inclusive) to high (exclusive), or if |
|
Return random floats in the half-open interval [0.0, 1.0). |
|
Generate a random sample from a given array. |
|
Return random bytes. |
Permutations¶
Modify an array in place by shuffling its contents. |
|
Randomly permute a sequence, or return a permuted range. |
|
Randomly permute x along axis axis. |
Distributions¶
Draw samples from a beta distribution. |
|
Draw samples from a binomial distribution. |
|
Draw samples from a chi-square distribution. |
|
Draw samples from the Dirichlet distribution. |
|
Draw samples from an exponential distribution. |
|
Draw samples from an F distribution. |
|
Draw samples from a gamma distribution. |
|
Draw samples from the geometric distribution. |
|
Draw samples from a Gumbel distribution. |
|
Draw samples from a hypergeometric distribution. |
|
Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay). |
|
Draw samples from a logistic distribution. |
|
Draw samples from a log-normal distribution. |
|
Draw samples from a logarithmic series distribution. |
|
Draw samples from a multinomial distribution. |
|
Generate variates from a multivariate hypergeometric distribution. |
|
Draw random samples from a multivariate normal distribution. |
|
Draw samples from a negative binomial distribution. |
|
Draw samples from a noncentral chi-square distribution. |
|
Draw samples from the noncentral F distribution. |
|
Draw samples from a normal (Gaussian) distribution. |
|
Draw samples from a Pareto II (AKA Lomax) distribution with the specified shape. |
|
Draw samples from a Poisson distribution. |
|
Draw samples in [0, 1] from a power distribution with positive exponent |
|
Draw samples from a Rayleigh distribution. |
|
Draw samples from a standard Cauchy distribution with mode = 0. |
|
Draw samples from the standard exponential distribution. |
|
Draw samples from a standard gamma distribution. |
|
Draw samples from a standard normal distribution. |
|
Draw samples from a standard Student's t distribution with df degrees of freedom. |
|
Draw samples from the triangular distribution over the interval [left, right]. |
|
Draw samples from a uniform distribution over [low, high). |
|
Draw samples from a von Mises distribution. |
|
Draw samples from a Wald, or inverse Gaussian, distribution. |
|
Draw samples from a Weibull distribution. |
|
Draw samples from a Zipf distribution. |
Bit generators¶
Base class for the bit generators of |
|
A Philox4x32-10 counter-based bit generator on the device. |
|
NumPy's PCG64DXSM bit generator on the device. |
|
An MRG32k3a combined multiple recursive bit generator on the device. |
|
SeedSequence mixes sources of entropy in a reproducible way to set the initial state for independent and very probably non-overlapping BitGenerators. |
Legacy random generation¶
RandomState keeps the parameter checks of NumPy's legacy one, also
for parameters in dpnp.ndarray, as CuPy's does, so that a call with
such parameters waits for the device to check them. The module-level
functions use one RandomState per SYCL queue: the
queue given by the device or sycl_queue keyword, or else the queue of
a dpnp.ndarray parameter, or else the default queue.
A container of a pseudo-random number generator on a SYCL device, with the legacy API of NumPy. |
|
Reseed the state of a queue. |
|
Return the state of a queue. |
|
Set the state of a queue. |
|
Return the bit generator of the state of a queue, which the |
|
Set the bit generator of the state of the queue of bitgen, which the |
Simple random data¶
Random values in a given shape, from a uniform distribution over [0, 1). |
|
Return a sample (or samples) from the standard normal distribution. |
|
Return random integers from low (inclusive) to high (exclusive). |
|
Random integers between low and high, inclusive. |
|
Return random floats in the half-open interval [0.0, 1.0). |
|
Return random floats in the half-open interval [0.0, 1.0). |
|
Return random floats in the half-open interval [0.0, 1.0). |
|
Return random floats in the half-open interval [0.0, 1.0). |
|
Generate a random sample from a given 1-D array. |
|
Return random bytes. |
Permutations¶
Modify an array in place by shuffling its contents along the first axis. |
|
Randomly permute a sequence, or return a permuted range. |
Distributions¶
Draw samples from a Beta distribution. |
|
Draw samples from a binomial distribution. |
|
Draw samples from a chi-square distribution. |
|
Draw samples from the Dirichlet distribution. |
|
Draw samples from an exponential distribution. |
|
Draw samples from an F distribution. |
|
Draw samples from a Gamma distribution. |
|
Draw samples from the geometric distribution. |
|
Draw samples from a Gumbel distribution. |
|
Draw samples from a hypergeometric distribution. |
|
Draw samples from the Laplace or double exponential distribution. |
|
Draw samples from a logistic distribution. |
|
Draw samples from a log-normal distribution. |
|
Draw samples from a logarithmic series distribution. |
|
Draw samples from a multinomial distribution. |
|
Draw random samples from a multivariate normal distribution. |
|
Draw samples from a negative binomial distribution. |
|
Draw samples from a noncentral chi-square distribution. |
|
Draw samples from the noncentral F distribution. |
|
Draw random samples from a normal (Gaussian) distribution. |
|
Draw samples from a Pareto II or Lomax distribution with specified shape. |
|
Draw samples from a Poisson distribution. |
|
Draws samples in [0, 1] from a power distribution with positive exponent a - 1. |
|
Draw samples from a Rayleigh distribution. |
|
Draw samples from a standard Cauchy distribution with mode = 0. |
|
Draw samples from the standard exponential distribution. |
|
Draw samples from a standard Gamma distribution. |
|
Draw samples from a standard Normal distribution (mean=0, stdev=1). |
|
Draw samples from a standard Student's t distribution with df degrees of freedom. |
|
Draw samples from the triangular distribution over the interval [left, right]. |
|
Draw samples from a uniform distribution over [low, high). |
|
Draw samples from a von Mises distribution. |
|
Draw samples from a Wald, or inverse Gaussian, distribution. |
|
Draw samples from a Weibull distribution. |
|
Draw samples from a Zipf distribution. |