For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
rand
def rand[dtype: DType](span: Span[Scalar[dtype]], /, *, min: Float64 = 0, max: Float64 = 1, int_scale: Optional[Int] = None)
Fills memory with random values from a uniform distribution.
Behavior depends on the dtype:
- Floating-point types sample values uniformly from [min, max).
- Integral types sample values uniformly from [min, max], clamped to the representable range of the dtype.
Parameters:
- dtype (
DType): The dtype of the pointer.
Args:
- span (
Span[Scalar[dtype]]): The memory area to fill. - min (
Float64): The lower bound of the range. - max (
Float64): The upper bound of the range. - int_scale (
Optional[Int]): Optional quantization scale for floating-point types. When provided, values are quantized to increments of 2^(-int_scale).
def rand[dtype: DType](ptr: UnsafePointer[Scalar[dtype], address_space=ptr.address_space], size: Int, /, *, min: Float64 = 0, max: Float64 = 1, int_scale: Optional[Int] = None)
Fills memory with random values from a uniform distribution.
Behavior depends on the dtype:
- Floating-point types sample values uniformly from [min, max).
- Integral types sample values uniformly from [min, max], clamped to the representable range of the dtype.
Example:
from std.random import rand, seed
seed()
var size: Int = 10
var data = List(length=size, fill=Float32(0))
rand(data, min=0.0, max=1.0, int_scale=16)
for i in range(size):
print(data[i]) # Random Float32 between 0.0 and 1.0
Parameters:
- dtype (
DType): The dtype of the pointer.
Args:
- ptr (
UnsafePointer[Scalar[dtype], address_space=ptr.address_space]): The pointer to the memory area to fill. - size (
Int): The number of elements to fill. - min (
Float64): The lower bound of the range. - max (
Float64): The upper bound of the range. - int_scale (
Optional[Int]): Optional quantization scale for floating-point types. When provided, values are quantized to increments of 2^(-int_scale).