lucid.random.rand

lucid.random.rand(*shape: int, requires_grad: bool = False, keep_grad: bool = False, device: Literal['cpu', 'gpu'] = 'cpu') Tensor
lucid.random.rand(shape: list[int] | tuple[int], requires_grad: bool = False, keep_grad: bool = False, device: Literal['cpu', 'gpu'] = 'cpu') Tensor

The rand function generates a tensor of the specified shape, filled with random values drawn from a uniform distribution over the interval \([0, 1)\).

Function Signature

def rand(
    shape: _ShapeLike,
    requires_grad: bool = False,
    keep_grad: bool = False,
    device: _DeviceType = "cpu",
) -> Tensor

Parameters

  • shape (int or tuple of int): The dimensions of the tensor to generate. Can be a single integer for a 1D tensor or a tuple for multidimensional tensors.

  • requires_grad (bool, optional): If set to True, the resulting tensor will track gradients for automatic differentiation. Defaults to False.

  • keep_grad (bool, optional): Determines whether gradient history should persist across multiple operations. Defaults to False.

Returns

  • Tensor: A tensor of shape shape with random values uniformly distributed in \([0, 1)\).

Example

>>> import lucid
>>> x = lucid.random.rand(2, 3)
>>> print(x)
Tensor([[0.64589411, 0.43758721, 0.891773  ],
        [0.96366276, 0.38344152, 0.79172504]], grad=None)

By default, the generated tensor does not track gradients. Set requires_grad=True to enable gradient tracking:

>>> y = lucid.random.rand(3, 2, requires_grad=True)
>>> print(y.requires_grad)
True

Note

  • The random values are drawn from a uniform distribution, which is suitable for initializing weights or general-purpose random number generation.

  • Use lucid.random.seed to ensure reproducibility of random values.