fn
rand
→Tensorrand(size: _int | tuple[_int, ...] = (), dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False, generator: _C_engine.Generator | None = None)Return a tensor of samples drawn from the continuous uniform distribution.
Each element is drawn independently from , the uniform distribution on the half-open unit interval:
The distribution has mean and variance .
Parameters
*sizeint or tuple[int, ...]Shape of the output tensor. Accepts varargs
rand(2, 3) or a
single tuple rand((2, 3)).dtypelucid.dtypeFloating-point data type. Defaults to the global default
(
lucid.float32). Integer dtypes are not supported.devicestr or lucid.deviceTarget device —
"cpu" or "metal".requires_gradboolIf
True, downstream operations are tracked by autograd.
Default: False.generatorlucid._C.engine.GeneratorExplicit generator to use. When
None (default), the global
default generator is used.Returns
TensorTensor of shape size with values in .
Notes
Internally the engine applies the Philox bijection and converts the raw 32-bit output to a float via the standard IEEE 754 mantissa trick:
yielding equally spaced values in with spacing .
Examples
>>> import lucid
>>> lucid.manual_seed(0)
>>> x = lucid.rand(3, 4)
>>> x.shape
(3, 4)
>>> float(x.min()) >= 0.0 and float(x.max()) < 1.0
True
Monte-Carlo estimate of $\pi$:
>>> lucid.manual_seed(0)
>>> n = 1_000_000
>>> pts = lucid.rand(n, 2)
>>> inside = ((pts ** 2).sum(dim=-1) < 1.0).float().mean()
>>> float(inside) * 4 # ≈ 3.1416