random

Module

Provides functions for random numbers.

You can import these APIs from the random package. For example:

from random import seed

seed

seed()

Seeds the random number generator using the current time.

seed(a: Int)

Seeds the random number generator using the value provided.

Args:

  • ​a (Int): The seed value.

random_float64

random_float64(min: SIMD[f64, 1], max: SIMD[f64, 1]) -> SIMD[f64, 1]

Returns a random Float64 number from the given range.

Args:

  • ​min (SIMD[f64, 1]): The minimum number in the range (default is 0.0).
  • ​max (SIMD[f64, 1]): The maximum number in the range (default is 1.0).

Returns:

A random number from the specified range.

random_si64

random_si64(min: SIMD[si64, 1], max: SIMD[si64, 1]) -> SIMD[si64, 1]

Returns a random Int64 number from the given range.

Args:

  • ​min (SIMD[si64, 1]): The minimum number in the range.
  • ​max (SIMD[si64, 1]): The maximum number in the range.

Returns:

A random number from the specified range.

random_ui64

random_ui64(min: SIMD[ui64, 1], max: SIMD[ui64, 1]) -> SIMD[ui64, 1]

Returns a random UInt64 number from the given range.

Args:

  • ​min (SIMD[ui64, 1]): The minimum number in the range.
  • ​max (SIMD[ui64, 1]): The maximum number in the range.

Returns:

A random number from the specified range.

randint

randint[type: DType](ptr: DTypePointer[type, 0], size: Int, low: Int, high: Int)

Fills memory with uniform random in range [low, high].

Constraints:

The type should be integral.

Parameters:

  • ​type (DType): The dtype of the pointer.

Args:

  • ​ptr (DTypePointer[type, 0]): The pointer to the memory area to fill.
  • ​size (Int): The number of elements to fill.
  • ​low (Int): The minimal value for random.
  • ​high (Int): The maximal value for random.

rand

rand[type: DType](ptr: DTypePointer[type, 0], size: Int)

Fills memory with random values from a uniform distribution.

Parameters:

  • ​type (DType): The dtype of the pointer.

Args:

  • ​ptr (DTypePointer[type, 0]): The pointer to the memory area to fill.
  • ​size (Int): The number of elements to fill.

rand[type: DType](*shape: Int) -> Tensor[type]

Constructs a new tensor with the specified shape and fills it with random elements.

Parameters:

  • ​type (DType): The dtype of the tensor.

Args:

  • ​shape (*Int): The tensor shape.

Returns:

A new tensor of specified shape and filled with random elements.

rand[type: DType](owned shape: TensorShape) -> Tensor[type]

Constructs a new tensor with the specified shape and fills it with random elements.

Parameters:

  • ​type (DType): The dtype of the tensor.

Args:

  • ​shape (TensorShape): The tensor shape.

Returns:

A new tensor of specified shape and filled with random elements.

rand[type: DType](owned spec: TensorSpec) -> Tensor[type]

Constructs a new tensor with the specified specification and fills it with random elements.

Parameters:

  • ​type (DType): The dtype of the tensor.

Args:

  • ​spec (TensorSpec): The tensor specification.

Returns:

A new tensor of specified specification and filled with random elements.

randn_float64

randn_float64(mean: SIMD[f64, 1], variance: SIMD[f64, 1]) -> SIMD[f64, 1]

Returns a random double sampled from Normal(mean, variance) distribution.

Args:

  • ​mean (SIMD[f64, 1]): Normal distribution mean.
  • ​variance (SIMD[f64, 1]): Normal distribution variance.

Returns:

A random float64 sampled from Normal(mean, variance).

randn

randn[type: DType](ptr: DTypePointer[type, 0], size: Int, mean: SIMD[f64, 1], variance: SIMD[f64, 1])

Fills memory with random values from a Normal(mean, variance) distribution.

Constraints:

The type should be floating point.

Parameters:

  • ​type (DType): The dtype of the pointer.

Args:

  • ​ptr (DTypePointer[type, 0]): The pointer to the memory area to fill.
  • ​size (Int): The number of elements to fill.
  • ​mean (SIMD[f64, 1]): Normal distribution mean.
  • ​variance (SIMD[f64, 1]): Normal distribution variance.

randn[type: DType](owned shape: TensorShape, mean: SIMD[f64, 1], variance: SIMD[f64, 1]) -> Tensor[type]

Constructs a new Tensor from the shape and fills it with random values from a Normal(mean, variance) distribution.

Constraints:

The type should be floating point.

Parameters:

  • ​type (DType): The dtype of the pointer.

Args:

  • ​shape (TensorShape): The shape of the Tensor to fill with random values.
  • ​mean (SIMD[f64, 1]): Normal distribution mean.
  • ​variance (SIMD[f64, 1]): Normal distribution variance.

Returns:

A Tensor filled with random dtype samples from Normal(mean, variance).

randn[type: DType](owned spec: TensorSpec, mean: SIMD[f64, 1], variance: SIMD[f64, 1]) -> Tensor[type]

Constructs a new Tensor from the spec and fills it with random values from a Normal(mean, variance) distribution.

Constraints:

The type should be floating point.

Parameters:

  • ​type (DType): The dtype of the pointer.

Args:

  • ​spec (TensorSpec): The spec of the Tensor to fill with random values.
  • ​mean (SIMD[f64, 1]): Normal distribution mean.
  • ​variance (SIMD[f64, 1]): Normal distribution variance.

Returns:

A Tensor filled with random dtype samples from Normal(mean, variance).