Python module
random
Provides random tensor generation utilities.
This module provides functions for generating random tensors with various distributions. All functions support specifying data type and device, with sensible defaults based on the target device.
You can generate random tensors using different distributions:
from max 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())gaussian()
max.random.gaussian(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)
Creates a tensor filled with random values from a Gaussian (normal) distribution.
Generates a tensor with values sampled from a normal (Gaussian) distribution with the specified mean and standard deviation. This is commonly used for weight initialization using techniques like Xavier/Glorot or He initialization.
Create tensors with random values from a Gaussian distribution:
from max import random
from max.driver import CPU
from max.dtype import DType
# Standard normal distribution
tensor = random.gaussian((2, 3), dtype=DType.float32, device=CPU())-
Parameters:
-
- shape (Iterable[int | str | Dim | integer[Any]]) – The shape of the output tensor. Defaults to scalar (empty tuple).
- mean (float) – The mean (center) of the Gaussian distribution. This determines
where the distribution is centered. Defaults to
0.0. - std (float) – The standard deviation (spread) of the Gaussian distribution.
Must be positive. Larger values create more spread in the distribution.
Defaults to
1.0. - 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 toNone. - device (Device | None) – The device where the tensor will be allocated. If
None, uses the default device (accelerator if available, otherwise CPU). Defaults toNone.
-
Returns:
-
A
Tensorwith random values sampled from the Gaussian distribution. -
Raises:
-
ValueError – If std <= 0.
normal()
max.random.normal(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)
Alias for gaussian().
Creates a tensor with values from a normal (Gaussian) distribution.
seed()
max.random.seed()
Gets the global random seed tensor.
Returns the global seed tensor used for random number generation in eager
execution mode. Creates the seed tensor on first access, initialized with
the dtype, shape, and device specified by ops.random.SeedType.
-
Returns:
-
The global seed tensor for random number generation.
-
Return type:
set_seed()
max.random.set_seed(value)
Sets the global random seed value.
Updates the global random seed to the specified value. This affects all subsequent random number generation in eager execution mode.
-
Parameters:
-
value (int) – The integer seed value to set.
-
Return type:
-
None
uniform()
max.random.uniform(shape=(), range=(0, 1), *, dtype=None, device=None)
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 import random
from max.dtype import DType
from max.driver import CPU
# Generate 2x3 tensor with values between 0 and 1
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]]) – 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 toNone. - device (Device | None) – The device where the tensor will be allocated. If
None, uses the default device (accelerator if available, otherwise CPU). Defaults toNone.
-
Returns:
-
A
Tensorwith random values sampled from the uniform distribution. -
Raises:
-
ValueError – If the range tuple does not contain exactly two values or if min >= max.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!