Random#
- class ivy.data_classes.array.random._ArrayWithRandom[source]#
Bases:
ABC
- _abc_impl = <_abc._abc_data object>#
- multinomial(population_size, num_samples, /, *, batch_size=1, replace=True, device=None, seed=None, out=None)[source]#
ivy.Array instance method variant of ivy.multinomial. This method simply wraps the function, and so the docstring for ivy.multinomial also applies to this method with minimal changes.
- Parameters:
self (
Array
) – The unnormalized probabilities for all elements in population, default is uniform [batch_shape, population_size]population_size (
int
) – The size of the population from which to draw samples.num_samples (
int
) – Number of independent samples to draw from the population.batch_size (
int
, default:1
) – Number of tensors to generate. Default is 1.replace (
bool
, default:True
) – Whether to replace samples once they’ve been drawn. Default isTrue
.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None)seed (
Optional
[int
], default:None
) – A python integer. Used to create a random seed distributionout (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Drawn samples from the parameterized normal distribution.
- randint(high, /, *, shape=None, device=None, dtype=None, seed=None, out=None)[source]#
ivy.Array instance method variant of ivy.randint. This method simply wraps the function, and so the docstring for ivy.randint also applies to this method with minimal changes.
- Parameters:
self (
Array
) – Lowest integer that can be drawn from the distribution.high (
Union
[int
,Array
,NativeArray
]) – One above the highest integer that can be drawn from the distribution.shape (
Optional
[Union
[Shape
,NativeShape
]], default:None
) – If the given shape is, e.g(m, n, k)
, thenm * n * k
samples are drawn. Can only be specified whenlow
andhigh
are numeric values, else exception will be raised. Default isNone
, where a single value is returned.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type will be the default integer data type. DefaultNone
seed (
Optional
[int
], default:None
) – A python integer. Used to create a random seed distributionout (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Returns an array with the given shape filled with integers from the uniform distribution in the “half-open” interval [low, high)
Examples
>>> x = ivy.array([[1, 2], [0, 5]]) >>> x.randint(10) ivy.array([[1, 5], [9, 7]])
>>> x.randint(8, device='cpu') ivy.array([[6, 5], [0, 5]])
>>> x.randint(9, dtype='int8') ivy.array([[1, 2], [7, 7]])
>>> x.randint(14, device='cpu', dtype='int16') ivy.array([[6, 5], [0, 5]])
>>> z = ivy.ones((2,2)) >>> x.randint(16, device='cpu', dtype='int64', out=z) ivy.array([[1, 2], [7, 7]])
>>> x = ivy.array([1, 2, 3]) >>> y = ivy.array([23, 25, 98]) >>> x.randint(y) ivy.array([ 5, 14, 18])
>>> x.randint(y, device='cpu') ivy.array([20, 13, 46])
>>> x.randint(y, dtype='int32') ivy.array([ 9, 18, 33])
>>> x.randint(y, device='cpu', dtype='int16') ivy.array([ 9, 20, 85])
>>> z = ivy.ones((3,)) >>> x.randint(y, device='cpu', dtype='int64', out=z) ivy.array([20, 13, 46])
- random_normal(*, std=1.0, shape=None, device=None, dtype=None, seed=None, out=None)[source]#
ivy.Array instance method variant of ivy.random_normal. This method simply wraps the function, and so the docstring for ivy.random_normal also applies to this method with minimal changes.
- Parameters:
self (
Array
) – The mean of the normal distribution to sample from. Default is0.0
.std (
Union
[float
,Array
,NativeArray
], default:1.0
) – The standard deviation of the normal distribution to sample from. Must be non-negative. Default is1.0
.shape (
Optional
[Union
[Shape
,NativeShape
]], default:None
) – If the given shape is, e.g(m, n, k)
, thenm * n * k
samples are drawn. Can only be specified whenmean
andstd
are numeric values, else exception will be raised. Default isNone
, where a single value is returned.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type will be the default floating-point data type. DefaultNone
seed (
Optional
[int
], default:None
) – A python integer. Used to create a random seed distributionout (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Drawn samples from the parameterized normal distribution.
Examples
>>> x = ivy.array([[9.8, 3.4], [5.8, 7.2]]) >>> x.random_normal(std=10.2) ivy.array([[19. , -6.44 ], [ 5.72 , 0.235]])
>>> x.random_normal(std=10.2, device='cpu') ivy.array([[18.7 , 25.2 ], [27.5 , -3.22]])
>>> x.random_normal(std=14.2, dtype='float16') ivy.array([[26.6 , 12.1 ], [ 4.56, 5.49]])
>>> x.random_normal(std=10.8, device='cpu', dtype='float64') ivy.array([[ 1.02, -1.39], [14.2 , -1. ]])
>>> z = ivy.ones((2,2)) >>> x.random_normal(std=11.2, device='cpu', dtype='float64', out=z) ivy.array([[ 7.72, -8.32], [ 4.95, 15.8 ]])
>>> x = ivy.array([8.7, 9.3]) >>> y = ivy.array([12.8, 14.5]) >>> x.random_normal(std=y) ivy.array([-10.8, 12.1])
>>> x.random_normal(std=y, device='cpu') ivy.array([ 13. , -26.9])
>>> x.random_normal(std=y, dtype='float16') ivy.array([14.3 , -0.807])
>>> x.random_normal(std=y, device='cpu', dtype='float64') ivy.array([21.3 , 3.85])
>>> z = ivy.ones((2,)) >>> x.random_normal(std=y, device='cpu', dtype='float64', out=z) ivy.array([ 4.32, 42.2 ])
- random_uniform(*, high=1.0, shape=None, device=None, dtype=None, seed=None, out=None)[source]#
ivy.Array instance method variant of ivy.random_uniform. This method simply wraps the function, and so the docstring for ivy.random_uniform also applies to this method with minimal changes.
- Parameters:
self (
Array
) – Lower boundary of the output interval. All values generated will be greater than or equal tolow
. If array, must have same shape ashigh
.high (
Union
[float
,Array
,NativeArray
], default:1.0
) – Upper boundary of the output interval. All the values generated will be less thanhigh
. If array, must have same shape aslow
.shape (
Optional
[Union
[Array
,Shape
,NativeShape
]], default:None
) – If the given shape is, e.g(m, n, k)
, thenm * n * k
samples are drawn. Can only be specified whenlow
andhigh
are numeric values, else exception will be raised. Default isNone
, where a single value is returned.device (
Optional
[Union
[Device
,NativeDevice
]], default:None
) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – output array data type. Ifdtype
isNone
, the output array data type will be the default floating-point data type. DefaultNone
seed (
Optional
[int
], default:None
) – A python integer. Used to create a random seed distributionout (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Drawn samples from the parameterized uniform distribution.
Examples
>>> x = ivy.array([[9.8, 3.4], [5.8, 7.2]]) >>> x.random_uniform(high=10.2) ivy.array([[9.86, 4.89], [7.06, 7.47]])
>>> x.random_uniform(high=10.2, device='cpu') ivy.array([[9.86, 4.89], [7.06, 7.47]])
>>> x.random_uniform(high=14.2, dtype='float16') ivy.array([[9.86, 4.89], [7.06, 7.47]])
>>> x.random_uniform(high=10.8, device='cpu', dtype='float64') ivy.array([[9.86, 4.89], [7.06, 7.47]])
>>> z = ivy.ones((2,2)) >>> x.random_uniform(high=11.2, device='cpu', dtype='float64', out=z) ivy.array([[10.1 , 6.53], [ 7.94, 8.85]])
>>> x = ivy.array([8.7, 9.3]) >>> y = ivy.array([12.8, 14.5]) >>> x.random_uniform(y) ivy.array([12.1, 14. ])
>>> x.random_uniform(high=y, device='cpu') ivy.array([12.1, 14. ])
>>> x.random_uniform(high=y, dtype='float16') ivy.array([12.1, 14. ])
>>> x.random_uniform(high=y, device='cpu', dtype='float64') ivy.array([12.1, 14. ])
>>> z = ivy.ones((2,)) >>> x.random_uniform(high=y, device='cpu', dtype='float64', out=z) ivy.array([12.1, 14. ])
- shuffle(axis=0, /, *, seed=None, out=None)[source]#
ivy.Array instance method variant of ivy.shuffle. This method simply wraps the function, and so the docstring for ivy.shuffle also applies to this method with minimal changes.
- Parameters:
self (
Array
) – Input array. Should have a numeric data type.axis (
Optional
[int
], default:0
) – The axis which x is shuffled along. Default is 0.seed (
Optional
[int
], default:None
) – A python integer. Used to create a random seed distributionout (
Optional
[Array
], default:None
) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – An array object, shuffled along the first dimension.
Examples
>>> x = ivy.array([5, 2, 9]) >>> y = x.shuffle() >>> print(y) ivy.array([2, 5, 9])
This should have hopefully given you an overview of the random submodule, if you have any questions, please feel free to reach out on our discord!