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 asx
. (usedtype
ifdtype
is not None) Ifhigh
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. Ifhigh
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. Ifhigh
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
), withshape
anddtype
. - Return type
-
Tensor
Examples
import paddle # example 1: # dtype is None and the dtype of x is float16 x = paddle.zeros((1,2)).astype("float16") out1 = paddle.randint_like(x, low=-5, high=5) print(out1) print(out1.dtype) # [[0, -3]] # random # paddle.float16 # example 2: # 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) print(out2.dtype) # [[0, -3]] # random # paddle.float32 # example 3: # dtype is None and the dtype of x is float64 x = paddle.zeros((1,2)).astype("float64") out3 = paddle.randint_like(x, low=-5, high=5) print(out3) print(out3.dtype) # [[0, -3]] # random # paddle.float64 # example 4: # dtype is None and the dtype of x is int32 x = paddle.zeros((1,2)).astype("int32") out4 = paddle.randint_like(x, low=-5, high=5) print(out4) print(out4.dtype) # [[0, -3]] # random # paddle.int32 # example 5: # dtype is None and the dtype of x is int64 x = paddle.zeros((1,2)).astype("int64") out5 = paddle.randint_like(x, low=-5, high=5) print(out5) print(out5.dtype) # [[0, -3]] # random # paddle.int64 # example 6: # dtype is float64 and the dtype of x is float32 x = paddle.zeros((1,2)).astype("float32") out6 = paddle.randint_like(x, low=-5, high=5, dtype="float64") print(out6) print(out6.dtype) # [[0, -1]] # random # paddle.float64 # example 7: # dtype is bool and the dtype of x is float32 x = paddle.zeros((1,2)).astype("float32") out7 = paddle.randint_like(x, low=-5, high=5, dtype="bool") print(out7) print(out7.dtype) # [[0, -1]] # random # paddle.bool # example 8: # dtype is int32 and the dtype of x is float32 x = paddle.zeros((1,2)).astype("float32") out8 = paddle.randint_like(x, low=-5, high=5, dtype="int32") print(out8) print(out8.dtype) # [[0, -1]] # random # paddle.int32 # example 9: # dtype is int64 and the dtype of x is float32 x = paddle.zeros((1,2)).astype("float32") out9 = paddle.randint_like(x, low=-5, high=5, dtype="int64") print(out9) print(out9.dtype) # [[0, -1]] # random # paddle.int64 # example 10: # dtype is int64 and the dtype of x is bool x = paddle.zeros((1,2)).astype("bool") out10 = paddle.randint_like(x, low=-5, high=5, dtype="int64") print(out10) print(out10.dtype) # [[0, -1]] # random # paddle.int64