IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0
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).

Rng

struct Rng

A seeded pseudo-random number generator.

Users should not need to create this type directly, instead use the Rng value provided by the Strategy trait.

Implemented traits​

AnyType, Deinitable, Movable

Methods​

rand_bool​

def rand_bool(mut self, *, true_probability: Float64 = 0.5) -> Bool

Returns a random Bool with the given probability of being True.

Args:

  • ​true_probability (Float64): The probability of being True (between 0.0 and 1.0).

Returns:

Bool: A random Bool.

Raises:

If the underlying random number generator raises an error.

rand_scalar​

def rand_scalar[dtype: DType](mut self, *, min: Scalar[dtype] = Scalar[dtype].MIN_FINITE, max: Scalar[dtype] = Scalar[dtype].MAX_FINITE) -> Scalar[dtype]

Returns a random Scalar from the given range.

Parameters:

  • ​dtype (DType): The DType of the scalar.

Args:

Returns:

Scalar[dtype]: A random number in the range [min, max].

Raises:

If the minimum value is greater than the maximum value or if the underlying random number generator raises an error.

rand_uint​

def rand_uint(mut self, *, min: UInt = UInt.MIN, max: UInt = UInt.MAX) -> UInt

Returns a random UInt from the given range.

Args:

  • ​min (UInt): The minimum value.
  • ​max (UInt): The maximum value.

Returns:

UInt: A random UInt in the range [min, max].

Raises:

If the underlying random number generator raises an error.

rand_int​

def rand_int(mut self, *, min: Int = Int.MIN, max: Int = Int.MAX) -> Int

Returns a random Int from the given range.

Args:

  • ​min (Int): The minimum value.
  • ​max (Int): The maximum value.

Returns:

Int: A random Int in the range [min, max].

Raises:

If the underlying random number generator raises an error.