mindspore.ops.uniform

View Source On Gitee
mindspore.ops.uniform(shape, minval, maxval, seed=None, dtype=mstype.float32)[source]

Generates random numbers according to the Uniform random number distribution.

Note

The number in tensor minval should be strictly less than maxval at any position after broadcasting.

Parameters
  • shape (Union[tuple, Tensor]) – The shape of returned tensor.

  • minval (Tensor) – Defines the minimum possible generated value.

  • maxval (Tensor) – Defines the maximum possible generated value.

  • seed (int) – Random number seed. Default None .

  • dtype (mindspore.dtype) – Type of the returned tensor.

Returns

Tensor

Supported Platforms:

GPU CPU

Examples

>>> import mindspore
>>> # For discrete uniform distribution, only one number is allowed for both minval and maxval:
>>> shape = (4, 2)
>>> minval = mindspore.tensor(1, mindspore.int32)
>>> maxval = mindspore.tensor(2, mindspore.int32)
>>> output = mindspore.ops.uniform(shape, minval, maxval, seed=5, dtype=mindspore.int32)
>>>
>>> # For continuous uniform distribution, minval and maxval can be multi-dimentional:
>>> shape = (3, 1, 2)
>>> minval = mindspore.tensor([[3, 4], [5, 6]], mindspore.float32)
>>> maxval = mindspore.tensor([8.0, 10.0], mindspore.float32)
>>> output = mindspore.ops.uniform(shape, minval, maxval, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 2)