randint_like

paddle. randint_like ( x, low=0, high=None, dtype=None, name=None ) [source]

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

Parameters
  • x (Tensor) – The input multi-dimensional tensor which specifies shape. The dtype of x can be bool, int32, int64, float16, float32, float64.

  • 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. If high is None, the range is [0, low).

  • dtype (str|np.dtype, optional) – The data type of the output tensor. Supported data types: bool, int32, int64, float16, float32, float64. If dytpe is None, the data type is the same as x’s data type. 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:
>>> # dtype is None and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out2 = paddle.randint_like(x, low=-5, high=5)
>>> print(out2)
>>> 
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0.]])
>>> 
>>> print(out2.dtype)
paddle.float32

>>> # example 2:
>>> # dtype is None and the dtype of x is float64
>>> x = paddle.zeros((1,2)).astype("float64")
>>> out2 = paddle.randint_like(x, low=-5, high=5)
>>> print(out2)
>>> 
Tensor(shape=[1, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
[[ 4., -5.]])
>>> 
>>> print(out2.dtype)
paddle.float64

>>> # example 3:
>>> # dtype is None and the dtype of x is int32
>>> x = paddle.zeros((1,2)).astype("int32")
>>> out3 = paddle.randint_like(x, low=-5, high=5)
>>> print(out3)
>>> 
Tensor(shape=[1, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[ 0, -4]])
>>> 
>>> print(out3.dtype)
paddle.int32

>>> # example 4:
>>> # dtype is None and the dtype of x is int64
>>> x = paddle.zeros((1,2)).astype("int64")
>>> out4 = paddle.randint_like(x, low=-5, high=5)
>>> print(out4)
>>> 
Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[ 4, -3]])
>>> 
>>> print(out4.dtype)
paddle.int64

>>> # example 5:
>>> # dtype is float64 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out5 = paddle.randint_like(x, low=-5, high=5, dtype="float64")
>>> print(out5)
>>> 
Tensor(shape=[1, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
[[3., 1.]])
>>> 
>>> print(out5.dtype)
paddle.float64

>>> # example 6:
>>> # dtype is bool and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out6 = paddle.randint_like(x, low=-5, high=5, dtype="bool")
>>> print(out6)
>>> 
Tensor(shape=[1, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
[[False, True ]])
>>> 
>>> print(out6.dtype)
paddle.bool

>>> # example 7:
>>> # dtype is int32 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out7 = paddle.randint_like(x, low=-5, high=5, dtype="int32")
>>> print(out7)
>>> 
Tensor(shape=[1, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[-2, -2]])
>>> 
>>> print(out7.dtype)
paddle.int32

>>> # example 8:
>>> # dtype is int64 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out8 = paddle.randint_like(x, low=-5, high=5, dtype="int64")
>>> print(out8)
>>> 
Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-5,  4]])
>>> 
>>> print(out8.dtype)
paddle.int64

>>> # example 9:
>>> # dtype is int64 and the dtype of x is bool
>>> x = paddle.zeros((1,2)).astype("bool")
>>> out9 = paddle.randint_like(x, low=-5, high=5, dtype="int64")
>>> print(out9)
>>> 
Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[ 1, -2]])
>>> 
>>> print(out9.dtype)
paddle.int64