Random Sampling (cupy.random
)¶
CuPy’s random number generation routines are based on cuRAND.
They cover a small fraction of numpy.random
.
The big difference of cupy.random
from numpy.random
is that cupy.random
supports dtype
option for most functions.
This option enables us to generate float32 values directly without any space overhead.
Sample random data¶
-
cupy.random.
choice
(a, size=None, replace=True, p=None)[source]¶ Returns an array of random values from a given 1-D array.
Each element of the returned array is independently sampled from
a
according top
or uniformly.Parameters: - a (1-D array-like or int) – If an array-like,
a random sample is generated from its elements.
If an int, the random sample is generated as if
a
wascupy.arange(n)
- size (int or tuple of ints) – The shape of the array.
- replace (boolean) – Whether the sample is with or without replacement
- p (1-D array-like) – The probabilities associated with each entry in
a
. If not given the sample assumes a uniform distribution over all entries ina
.
Returns: - An array of
a
values distributed according to p
or uniformly.
Return type: See also
- a (1-D array-like or int) – If an array-like,
a random sample is generated from its elements.
If an int, the random sample is generated as if
-
cupy.random.
rand
(*size, **kwarg)[source]¶ Returns an array of uniform random values over the interval
[0, 1)
.Each element of the array is uniformly distributed on the half-open interval
[0, 1)
. All elements are identically and independently distributed (i.i.d.).Parameters: - size (tuple of ints) – The shape of the array.
- dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed. The default isnumpy.float64
.
Returns: A random array.
Return type: See also
-
cupy.random.
randn
(*size, **kwarg)[source]¶ Returns an array of standard normal random values.
Each element of the array is normally distributed with zero mean and unit variance. All elements are identically and independently distributed (i.i.d.).
Parameters: - size (tuple of ints) – The shape of the array.
- dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed. The default isnumpy.float64
.
Returns: An array of standard normal random values.
Return type: See also
-
cupy.random.
randint
(low, high=None, size=None)[source]¶ Returns a scalar or an array of integer values over
[low, high)
.Each element of returned values are independently sampled from uniform distribution over left-close and right-open interval
[low, high)
.Parameters: Returns: If size is
None
, it is single integer sampled. If size is integer, it is the 1D-array of lengthsize
element. Otherwise, it is the array whose shape specified bysize
.Return type: int or cupy.ndarray of ints
-
cupy.random.
random_integers
(low, high=None, size=None)[source]¶ Return a scalar or an array of integer values over
[low, high]
Each element of returned values are independently sampled from uniform distribution over closed interval
[low, high]
.Parameters: Returns: If size is
None
, it is single integer sampled. If size is integer, it is the 1D-array of lengthsize
element. Otherwise, it is the array whose shape specified bysize
.Return type: int or cupy.ndarray of ints
-
cupy.random.
random_sample
(size=None, dtype=<type 'float'>)[source]¶ Returns an array of random values over the interval
[0, 1)
.This is a variant of
cupy.random.rand()
.Parameters: - size (int or tuple of ints) – The shape of the array.
- dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed.
Returns: An array of uniformly distributed random values.
Return type: See also
-
cupy.random.
random
(size=None, dtype=<type 'float'>)¶ Returns an array of random values over the interval
[0, 1)
.This is a variant of
cupy.random.rand()
.Parameters: - size (int or tuple of ints) – The shape of the array.
- dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed.
Returns: An array of uniformly distributed random values.
Return type: See also
-
cupy.random.
ranf
(size=None, dtype=<type 'float'>)¶ Returns an array of random values over the interval
[0, 1)
.This is a variant of
cupy.random.rand()
.Parameters: - size (int or tuple of ints) – The shape of the array.
- dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed.
Returns: An array of uniformly distributed random values.
Return type: See also
-
cupy.random.
sample
(size=None, dtype=<type 'float'>)¶ Returns an array of random values over the interval
[0, 1)
.This is a variant of
cupy.random.rand()
.Parameters: - size (int or tuple of ints) – The shape of the array.
- dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed.
Returns: An array of uniformly distributed random values.
Return type: See also
Distributions¶
-
cupy.random.
gumbel
(loc=0.0, scale=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of samples drawn from a Gumbel distribution.
The samples are drawn from a Gumbel distribution with location
loc
and scalescale
. Its probability density function is defined as\[f(x) = \frac{1}{\eta} \exp\left\{ - \frac{x - \mu}{\eta} \right\} \exp\left[-\exp\left\{-\frac{x - \mu}{\eta} \right\}\right],\]where \(\mu\) is
loc
and \(\eta\) isscale
.Parameters: Returns: Samples drawn from the Gumbel destribution.
Return type: See also
-
cupy.random.
lognormal
(mean=0.0, sigma=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of samples drawn from a log normal distribution.
The samples are natural log of samples drawn from a normal distribution with mean
mean
and deviationsigma
.Parameters: Returns: Samples drawn from the log normal distribution.
Return type: See also
-
cupy.random.
normal
(loc=0.0, scale=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of normally distributed samples.
Parameters: - loc (float or array_like of floats) – Mean of the normal distribution.
- scale (float or array_like of floats) – Standard deviation of the normal distribution.
- size (int or tuple of ints) – The shape of the array. If
None
, a zero-dimensional array is generated. - dtype – Data type specifier. Only
numpy.float32
andnumpy.float64
types are allowed.
Returns: Normally distributed samples.
Return type: See also
-
cupy.random.
standard_normal
(size=None, dtype=<type 'float'>)[source]¶ Returns an array of samples drawn from the standard normal distribution.
This is a variant of
cupy.random.randn()
.Parameters: - size (int or tuple of ints) – The shape of the array. If
None
, a zero-dimensional array is generated. - dtype – Data type specifier.
Returns: Samples drawn from the standard normal distribution.
Return type: See also
- size (int or tuple of ints) – The shape of the array. If
-
cupy.random.
uniform
(low=0.0, high=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of uniformly-distributed samples over an interval.
Samples are drawn from a uniform distribution over the half-open interval
[low, high)
.Parameters: Returns: Samples drawn from the uniform distribution.
Return type: See also
Random number generator¶
-
cupy.random.
seed
(seed=None)[source]¶ Resets the state of the random number generator with a seed.
This function resets the state of the global random number generator for the current device. Be careful that generators for other devices are not affected.
Parameters: seed (None or int) – Seed for the random number generator. If None
, it usesos.urandom()
if available ortime.clock()
otherwise. Note that this function does not support seeding by an integer array.
-
cupy.random.
get_random_state
()[source]¶ Gets the state of the random number generator for the current device.
If the state for the current device is not created yet, this function creates a new one, initializes it, and stores it as the state for the current device.
Returns: The state of the random number generator for the device. Return type: RandomState
-
class
cupy.random.
RandomState
(seed=None, method=100)[source]¶ Portable container of a pseudo-random number generator.
An instance of this class holds the state of a random number generator. The state is available only on the device which has been current at the initialization of the instance.
Functions of
cupy.random
use global instances of this class. Different instances are used for different devices. The global state for the current device can be obtained by thecupy.random.get_random_state()
function.Parameters: - seed (None or int) – Seed of the random number generator. See the
seed()
method for detail. - method (int) –
Method of the random number generator. Following values are available:
cupy.cuda.curand.CURAND_RNG_PSEUDO_DEFAULT cupy.cuda.curand.CURAND_RNG_XORWOW cupy.cuda.curand.CURAND_RNG_MRG32K3A cupy.cuda.curand.CURAND_RNG_MTGP32 cupy.cuda.curand.CURAND_RNG_MT19937 cupy.cuda.curand.CURAND_RNG_PHILOX4_32_10
-
choice
(a, size=None, replace=True, p=None)[source]¶ Returns an array of random values from a given 1-D array.
See also
cupy.random.choice()
for full document,numpy.random.choice()
-
interval
(mx, size)[source]¶ Generate multiple integers independently sampled uniformly from
[0, mx]
.Parameters: Returns: If
None
, ancupy.ndarray
with shape()
is returned. Ifint
, 1-D array of length size is returned. Iftuple
, multi-dimensional array with shapesize
is returned. Currently, each element of the array isnumpy.int32
.Return type: int or cupy.ndarray
-
lognormal
(mean=0.0, sigma=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of samples drawn from a log normal distribution.
See also
cupy.random.lognormal()
for full documentation,numpy.random.RandomState.lognormal()
-
normal
(loc=0.0, scale=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of normally distributed samples.
See also
cupy.random.normal()
for full documentation,numpy.random.RandomState.normal()
-
rand
(*size, **kwarg)[source]¶ Returns uniform random values over the interval
[0, 1)
.See also
cupy.random.rand()
for full documentation,numpy.random.RandomState.rand()
-
randn
(*size, **kwarg)[source]¶ Returns an array of standard normal random values.
See also
cupy.random.randn()
for full documentation,numpy.random.RandomState.randn()
-
random_sample
(size=None, dtype=<type 'float'>)[source]¶ Returns an array of random values over the interval
[0, 1)
.See also
cupy.random.random_sample()
for full documentation,numpy.random.RandomState.random_sample()
-
seed
(seed=None)[source]¶ Resets the state of the random number generator with a seed.
See also
cupy.random.seed()
for full documentation,numpy.random.RandomState.seed()
-
standard_normal
(size=None, dtype=<type 'float'>)[source]¶ Returns samples drawn from the standard normal distribution.
See also
cupy.random.standard_normal()
for full documentation,numpy.random.RandomState.standard_normal()
-
uniform
(low=0.0, high=1.0, size=None, dtype=<type 'float'>)[source]¶ Returns an array of uniformly-distributed samples over an interval.
See also
cupy.random.uniform()
for full documentation,numpy.random.RandomState.uniform()
- seed (None or int) – Seed of the random number generator. See the