Skip to main content

Python function

uniform

uniform()

max.experimental.random.uniform(shape=(), range=(0, 1), *, dtype=None, device=None)

source

Creates a tensor filled with random values from a uniform distribution.

Generates a tensor with values uniformly distributed between the specified minimum and maximum bounds. This is useful for initializing weights, generating random inputs, or creating noise.

Create tensors with uniform random values:

from max.experimental import random
from max.dtype import DType
from max.driver import CPU

tensor1 = random.uniform((2, 3), dtype=DType.float32, device=CPU())

tensor2 = random.uniform((4, 4), range=(0, 1), dtype=DType.float32, device=CPU())

Parameters:

  • shape (Iterable[int | str | Dim | integer[Any] | TypedAttr]) – The shape of the output tensor. Defaults to scalar (empty tuple).
  • range (tuple[float, float]) – A tuple specifying the (min, max) bounds of the uniform distribution. The minimum value is inclusive, the maximum value is exclusive. Defaults to (0, 1).
  • dtype (DType | None) – The data type of the output tensor. If None, uses the default dtype for the specified device (float32 for CPU, bfloat16 for accelerators). Defaults to None.
  • device (Device | None) – The device where the tensor will be allocated. If None, uses the default device (accelerator if available, otherwise CPU). Defaults to None.

Returns:

A Tensor with random values sampled from the uniform distribution.

Raises:

ValueError – If the range tuple does not contain exactly two values or if min >= max.