paddle.fluid.layers.randint(low, high=None, shape=None, out=None, dtype=None, device=None, stop_gradient=False, seed=0, name=None)[source]

This function returns a Tensor filled with random integers from the “discrete uniform” distribution of the specified data type in the interval [low, high). If high is None (the default), then results are from [0, low).

  • low (int) – The lower bound on the range of random values to generate, the low is included in the range. (unless high=None, in which case this parameter is one above the highest such integer).

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

  • shape (list|tuple|Variable, optional) – The shape of the output Tensor, if the shape is a list or tuple, its elements can be an integer or a Tensor with the shape [1], and the type of the Tensor must be int32 or int64. If the shape is a Variable, it is a 1-D Tensor, and the type of the Tensor must be int32 or int64. Default is None, in which case the shape is [1].

  • out (Variable, optional) – Optional output which can be any created Variable that meets the requirements to store the result of operation. if out is None, a new Varibale will be create to store the result.

  • dtype (np.dtype|core.VarDesc.VarType|str, optional) – Data type of the output Tensor which can be int32, int64, if dytpe is None, the data type of created Tensor is int64

  • device (str, optional) – This parameter specifies that the Tensor is created on the GPU or CPU.

  • stop_gradient (bool, optional) – Indicating if we stop gradient from current(out) Variable, default value is False.

  • seed (int, optional) – Random seed used for permute samples. If seed is equal to 0, it means use a seed generated by the system. Note that if seed is not 0, this operator will always generate the same random permutation every time. Default: 0.

  • 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.


A Tensor of the specified shape filled with random integers.

Return type



TypeError – Randint’s low must less then high.


System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/layers/nn.py:docstring of paddle.fluid.layers.randint, line 46)

Error in “code-block” directive: maximum 1 argument(s) allowed, 5 supplied.

.. code-block:: python
    import paddle.fluid as fluid

    # example 1:
    # attr shape is a list which doesn't contain tensor Variable.
    result_1 = fluid.layers.randint(low=-5, high=5, shape=[3, 4], dtype="int64")

    # example 2:
    # attr shape is a list which contains tensor Variable.
    dim_1 = fluid.layers.fill_constant([1],"int64",3)
    dim_2 = fluid.layers.fill_constant([1],"int32",5)
    result_2 = fluid.layers.randint(low=-5, high=5, shape=[dim_1, dim_2], dtype="int32")

    # example 3:
    # attr shape is a Variable, the data type must be int64 or int32.
    var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64")
    result_3 = fluid.layers.randint(low=-5, high=5, shape=var_shape, dtype="int32")
    var_shape_int32 = fluid.data(name='var_shape_int32', shape=[2], dtype="int32")
    result_4 = fluid.layers.randint(low=-5, high=5, shape=var_shape_int32, dtype="int64")

    # example 4:
    # Input only one parameter
    # low=0, high=10, shape=[1], dtype='int64'
    result_4 = fluid.layers.randint(10)