randint

paddle. randint ( low=0, high=None, shape=[1], dtype=None, name=None ) [source]

Returns a Tensor filled with random integers from a discrete uniform distribution in the range [low, high), with shape and dtype. If high is None (the default), the range is [0, low).

Parameters
  • low (int, optional) – The lower bound on the range of random values to generate. The low is included in the range. If high is None, the range is [0, low). Default is 0.

  • high (int, optional) – The upper bound on the range of random values to generate, the high is excluded in the range. Default is None (see above for behavior if high = None). Default is None.

  • shape (tuple|list|Tensor) – Shape of the Tensor to be created. The data type is int32 or int64 . If shape is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. If shape is an Tensor, it should be an 1-D Tensor which represents a list. Default is [1].

  • dtype (str|np.dtype, optional) – The data type of the output tensor. Supported data types: int32, int64. If dtype is None, the data type is int64. Default is None.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

A Tensor filled with random integers from a discrete uniform distribution in the range [low, high), with shape and dtype.

Return type

Tensor

Examples

>>> import paddle

>>> # example 1:
>>> # attr shape is a list which doesn't contain Tensor.
>>> out1 = paddle.randint(low=-5, high=5, shape=[2, 3])
>>> print(out1)
>>> 
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-1,  4,  4],
 [-2, -5, -2]])
>>> 

>>> # example 2:
>>> # attr shape is a list which contains Tensor.
>>> dim1 = paddle.to_tensor(2, 'int64')
>>> dim2 = paddle.to_tensor(3, 'int32')
>>> out2 = paddle.randint(low=-5, high=5, shape=[dim1, dim2])
>>> print(out2)
>>> 
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-4, -4,  2],
 [-3, -1, -5]])
>>> 

>>> # example 3:
>>> # attr shape is a Tensor
>>> shape_tensor = paddle.to_tensor([2, 3])
>>> out3 = paddle.randint(low=-5, high=5, shape=shape_tensor)
>>> print(out3)
>>> 
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-1,  4, -3],
 [ 1,  2, -1]])
>>> 

>>> # example 4:
>>> # data type is int32
>>> out4 = paddle.randint(low=-5, high=5, shape=[3], dtype='int32')
>>> print(out4)
>>> 
Tensor(shape=[3], dtype=int32, place=Place(cpu), stop_gradient=True,
[4, 4, 0])
>>> 

>>> # example 5:
>>> # Input only one parameter
>>> # low=0, high=10, shape=[1], dtype='int64'
>>> out5 = paddle.randint(10)
>>> print(out5)
>>> 
Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
[7])
>>>