Random sampling (cupy.random)#

Differences between cupy.random and numpy.random:

  • Most functions under cupy.random support the dtype option, which do not exist in the corresponding NumPy APIs. This option enables generation of float32 values directly without any space overhead.

  • cupy.random.default_rng() uses XORWOW bit generator by default.

  • Random states cannot be serialized. See the description below for details.

  • CuPy does not guarantee that the same number generator is used across major versions. This means that numbers generated by cupy.random by new major version may not be the same as the previous one, even if the same seed and distribution are used.

New Random Generator API#

Random Generator#

default_rng([seed])

Construct a new Generator with the default BitGenerator (XORWOW).

Generator(bit_generator)

Container for the BitGenerators.

Bit Generators#

BitGenerator([seed])

Generic BitGenerator.

CuPy provides the following bit generator implementations:

XORWOW([seed, size])

BitGenerator that uses cuRAND XORWOW device generator.

MRG32k3a([seed, size])

BitGenerator that uses cuRAND MRG32k3a device generator.

Philox4x3210([seed, size])

BitGenerator that uses cuRAND Philox4x3210 device generator.

Legacy Random Generation#

RandomState([seed, method])

Portable container of a pseudo-random number generator.

Functions in cupy.random#

beta(a, b[, size, dtype])

Beta distribution.

binomial(n, p[, size, dtype])

Binomial distribution.

bytes(length)

Returns random bytes.

chisquare(df[, size, dtype])

Chi-square distribution.

choice(a[, size, replace, p])

Returns an array of random values from a given 1-D array.

dirichlet(alpha[, size, dtype])

Dirichlet distribution.

exponential(scale[, size, dtype])

Exponential distribution.

f(dfnum, dfden[, size, dtype])

F distribution.

gamma(shape[, scale, size, dtype])

Gamma distribution.

geometric(p[, size, dtype])

Geometric distribution.

gumbel([loc, scale, size, dtype])

Returns an array of samples drawn from a Gumbel distribution.

hypergeometric(ngood, nbad, nsample[, size, ...])

hypergeometric distribution.

laplace([loc, scale, size, dtype])

Laplace distribution.

logistic([loc, scale, size, dtype])

Logistic distribution.

lognormal([mean, sigma, size, dtype])

Returns an array of samples drawn from a log normal distribution.

logseries(p[, size, dtype])

Log series distribution.

multinomial(n, pvals[, size])

Returns an array from multinomial distribution.

multivariate_normal(mean, cov[, size, ...])

Multivariate normal distribution.

negative_binomial(n, p[, size, dtype])

Negative binomial distribution.

noncentral_chisquare(df, nonc[, size, dtype])

Noncentral chisquare distribution.

noncentral_f(dfnum, dfden, nonc[, size, dtype])

Noncentral F distribution.

normal([loc, scale, size, dtype])

Returns an array of normally distributed samples.

pareto(a[, size, dtype])

Pareto II or Lomax distribution.

permutation(a)

Returns a permuted range or a permutation of an array.

poisson([lam, size, dtype])

Poisson distribution.

power(a[, size, dtype])

Power distribution.

rand(*size, **kwarg)

Returns an array of uniform random values over the interval [0, 1).

randint(low[, high, size, dtype])

Returns a scalar or an array of integer values over [low, high).

randn(*size, **kwarg)

Returns an array of standard normal random values.

random([size, dtype])

Returns an array of random values over the interval [0, 1).

random_integers(low[, high, size])

Return a scalar or an array of integer values over [low, high]

random_sample([size, dtype])

Returns an array of random values over the interval [0, 1).

ranf([size, dtype])

Returns an array of random values over the interval [0, 1).

rayleigh([scale, size, dtype])

Rayleigh distribution.

sample([size, dtype])

Returns an array of random values over the interval [0, 1).

seed([seed])

Resets the state of the random number generator with a seed.

shuffle(a)

Shuffles an array.

standard_cauchy([size, dtype])

Standard cauchy distribution.

standard_exponential([size, dtype])

Standard exponential distribution.

standard_gamma(shape[, size, dtype])

Standard gamma distribution.

standard_normal([size, dtype])

Returns an array of samples drawn from the standard normal distribution.

standard_t(df[, size, dtype])

Standard Student's t distribution.

triangular(left, mode, right[, size, dtype])

Triangular distribution.

uniform([low, high, size, dtype])

Returns an array of uniformly-distributed samples over an interval.

vonmises(mu, kappa[, size, dtype])

von Mises distribution.

wald(mean, scale[, size, dtype])

Wald distribution.

weibull(a[, size, dtype])

weibull distribution.

zipf(a[, size, dtype])

Zipf distribution.

CuPy does not provide cupy.random.get_state nor cupy.random.set_state at this time. Use the following CuPy-specific APIs instead. Note that these functions use cupy.random.RandomState instance to represent the internal state, which cannot be serialized.

get_random_state()

Gets the state of the random number generator for the current device.

set_random_state(rs)

Sets the state of the random number generator for the current device.