randint#

ivy.randint(low, high, /, *, shape=None, device=None, dtype=None, seed=None, out=None)[source]#

Return an array filled with random integers generated uniformly between low (inclusive) and high (exclusive).

Parameters:
  • low (Union[int, NativeArray, Array]) – Lowest integer that can be drawn from the distribution.

  • high (Union[int, NativeArray, Array]) – 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), 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 integer 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 integers from the uniform distribution in the “half-open” interval [low, high)

Examples

>>> y = ivy.randint(0, 9, shape=(1,1))
>>> print(y)
ivy.array([[5]])
>>> y = ivy.randint(2, 20, shape=(2, 2), device='cpu', seed=42)
>>> print(y)
ivy.array([[ 8, 16],
           [12,  9]])
>>> x = ivy.array([1, 2, 3])
>>> ivy.randint(0, 10, shape=(3,), out=x)
>>> print(x)
ivy.array([2, 6, 7])
>>> y = ivy.zeros((3, 3))
>>> ivy.randint(3, 15, shape=(3, 3), device='cpu', out=y)
>>> print(y)
ivy.array([[ 7,  7,  5],
           [12,  8,  8],
           [ 8, 11,  3]])
Array.randint(self, 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), then m * n * k samples are drawn. Can only be specified when low and high 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 integer 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 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])
Container.randint(self, high, /, *, shape=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, device=None, dtype=None, seed=None, out=None)[source]#

ivy.Container 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 (Container) – Lowest integer that can be drawn from the distribution.

  • high (Union[int, Container, Array, NativeArray]) – One above the highest integer that can be drawn from the distribution.

  • shape (Optional[Union[Shape, NativeShape, Container]], default: None) – If the given shape is, e.g (m, n, k), then m * n * k samples are drawn. Can only be specified when low and high are numeric values, else exception will be raised. Default is None, where a single value is returned.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • device (Optional[Union[Device, NativeDevice, Container]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).

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

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

  • out (Optional[Container], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

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.Container(a=ivy.array([7,6,0]),
...                   b=ivy.array([8,9,4]))
>>> x.randint(30)
{
    a: ivy.array([23, 15, 20]),
    b: ivy.array([28, 22, 18])
}
>>> x.randint(10, device='cpu')
{
    a: ivy.array([9, 7, 7]),
    b: ivy.array([8, 9, 9])
}
>>> x.randint(102, dtype='int8')
{
    a: ivy.array([9, 8, 2]),
    b: ivy.array([62, 62, 60])
}
>>> x.randint(54, device='cpu', dtype='int64')
{
    a: ivy.array([30, 29, 26]),
    b: ivy.array([24, 24, 21])
}
>>> z = ivy.Container(a=ivy.zeros((3,)), b=ivy.ones((3,)))
>>> x.randint(21, device='cpu', dtype='int8', out=z)
{
    a: ivy.array([7, 6, 0]),
    b: ivy.array([8, 9, 4])
}
>>> y = ivy.Container(a=54, b=17)
>>> x.randint(y)
{
    a: ivy.array([7, 6, 0]),
    b: ivy.array([8, 9, 4])
}
>>> x.randint(y, device='cpu')
{
    a: ivy.array([7, 6, 0]),
    b: ivy.array([8, 9, 4])
}
>>> x.randint(y, dtype='int64')
{
    a: ivy.array([7, 6, 0]),
    b: ivy.array([8, 9, 4])
}
>>> x.randint(y, device='cpu', dtype='int32')
{
    a: ivy.array([7, 6, 0]),
    b: ivy.array([8, 9, 4])
}
>>> z = ivy.Container(a=ivy.zeros((3,)), b=ivy.ones((3,)))
>>> x.randint(y, device='cpu', dtype='int16', out=z)
{
    a: ivy.array([7, 6, 0]),
    b: ivy.array([8, 9, 4])
}
>>> x = ivy.Container(a=ivy.array([[9,7],[6,2]]),
...                   b=ivy.array([[0,2],[10,6]]))
>>> y = ivy.Container(a=ivy.array([[10,32],[18,19]]),
...                   b=ivy.array([[44,5],[23,54]]))
>>> x.randint(y)
{
    a: ivy.array([[9, 7],
                  [6, 2]]),
    b: ivy.array([[0, 2],
                  [10, 6]])
}
>>> x.randint(y, device='cpu')
{
    a: ivy.array([[9, 7],
                  [6, 2]]),
    b: ivy.array([[0, 2],
                  [10, 6]])
}
>>> x.randint(y, dtype='int64')
{
    a: ivy.array([[9, 7],
                  [6, 2]]),
    b: ivy.array([[0, 2],
                  [10, 6]])
}
>>> x.randint(y, device='cpu', dtype='int32')
{
    a: ivy.array([[9, 7],
                  [6, 2]]),
    b: ivy.array([[0, 2],
                  [10, 6]])
}
>>> z = ivy.Container(a=ivy.zeros((2,2)), b=ivy.ones((2,2)))
>>> x.randint(y, device='cpu', dtype='int16', out=z)
{
    a: ivy.array([[9, 7],
                  [6, 2]]),
    b: ivy.array([[0, 2],
                  [10, 6]])
}