Random#

ivy.bernoulli(probs, *, logits=None, shape=None, device=None, dtype=None, seed=None, out=None)[source]#

Draws samples from Bernoulli distribution parameterized by probs or logits (but not both)

Parameters:
  • logits (Optional[Union[float, NativeArray, Array]], default: None) – An N-D Array representing the log-odds of a 1 event. Each entry in the Array parameterizes an independent Bernoulli distribution where the probability of an event is sigmoid (logits). Only one of logits or probs should be passed in.

  • probs (Union[float, Array, NativeArray]) – An N-D Array representing the probability of a 1 event. Each entry in the Array parameterizes an independent Bernoulli distribution. Only one of logits or probs should be passed in

  • shape (Optional[Union[Shape, NativeShape]], default: None) – If the given shape is, e.g ‘(m, n, k)’, then ‘m * n * k’ samples are drawn. (Default value = ‘None’, where ‘ivy.shape(logits)’ samples are drawn)

  • 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. If dtype is None, the output array data type will be the default floating-point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (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 Bernoulli distribution

ivy.beta(a, b, /, *, shape=None, device=None, dtype=None, seed=None, out=None)[source]#

Return an array filled with random values sampled from a beta distribution.

Parameters:
  • a (Union[float, NativeArray, Array]) – Alpha parameter of the beta distribution.

  • b (Union[float, NativeArray, Array]) – Beta parameter of the beta distribution.

  • shape (Optional[Union[Shape, NativeShape]], default: None) – If the given shape is, e.g (m, n, k), then m * n * k samples are drawn Can only be specified when mean and std are numeric values, else exception will be raised. Default is None, 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. If dtype is None, the output array data type will be the default floating point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (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 random values sampled from a beta distribution.

ivy.dirichlet(alpha, /, *, size=None, dtype=None, seed=None, out=None)[source]#

Draw size samples of dimension k from a Dirichlet distribution. A Dirichlet- distributed random variable can be seen as a multivariate generalization of a Beta distribution. The Dirichlet distribution is a conjugate prior of a multinomial distribution in Bayesian inference.

Parameters:
  • alpha (Union[Array, NativeArray, float, Sequence[float]]) – Sequence of floats of length k

  • size (Optional[Union[Shape, NativeShape]], default: None) – optional int or tuple of ints, Output shape. If the given shape is, e.g., (m, n), then m * n * k samples are drawn. Default is None, in which case a vector of length k is returned.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type will be the default floating-point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (Optional[Array], default: None) – optional output array, for writing the result to.

Return type:

Array

Returns:

ret – The drawn samples, of shape (size, k).

Examples

>>> alpha = [1.0, 2.0, 3.0]
>>> ivy.dirichlet(alpha)
ivy.array([0.10598304, 0.21537054, 0.67864642])
>>> alpha = [1.0, 2.0, 3.0]
>>> ivy.dirichlet(alpha, size = (2,3))
ivy.array([[[0.48006698, 0.07472073, 0.44521229],
    [0.55479872, 0.05426367, 0.39093761],
    [0.19531053, 0.51675832, 0.28793114]],
[[0.12315625, 0.29823365, 0.5786101 ],

[0.15564976, 0.50542368, 0.33892656], [0.1325352 , 0.44439589, 0.42306891]]])

ivy.gamma(alpha, beta, /, *, shape=None, device=None, dtype=None, seed=None, out=None)[source]#

Return an array filled with random values sampled from a gamma distribution.

Parameters:
  • alpha (Union[float, NativeArray, Array]) – Alpha parameter of the gamma distribution.

  • beta (Union[float, NativeArray, Array]) – Beta parameter of the gamma distribution.

  • shape (Optional[Union[float, NativeArray, Array]], default: None) – Shape parameter of the gamma distribution.

  • 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. If dtype is None, the output array data type will be the default floating point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (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 filled with random values sampled from a gamma distribution.

ivy.poisson(lam, *, shape=None, device=None, dtype=None, seed=None, fill_value=0, out=None)[source]#

Draws samples from a poisson distribution.

Parameters:
  • lam (Union[float, Array, NativeArray]) – Rate parameter(s) describing the poisson distribution(s) to sample. It must have a shape that is broadcastable to the requested shape.

  • shape (Optional[Union[Shape, NativeShape]], default: None) – If the given shape is, e.g ‘(m, n, k)’, then ‘m * n * k’ samples are drawn. (Default value = ‘None’, where ‘ivy.shape(lam)’ samples are drawn)

  • 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. If dtype is None, the output array data type will be the default floating-point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution.

  • fill_value (Optional[Union[int, float]], default: 0) – if lam is negative, fill the output array with this value on that specific dimension.

  • out (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 poisson distribution

Examples

>>> lam = [1.0, 2.0, 3.0]
>>> ivy.poisson(lam)
ivy.array([1., 4., 4.])
>>> lam = [1.0, 2.0, 3.0]
>>> ivy.poisson(lam, shape = (2,3))
ivy.array([[0., 2., 2.],
           [1., 2., 3.]])

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!