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.0b2
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).

randint

def randint[dtype: DType](span: Span[Scalar[dtype]], low: Int, high: Int) where dtype.is_integral()

Fills memory with uniformly distributed random integers in the range [low, high].

Constraints:

The type must be integral.

Parameters:

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

Args:

  • span (Span[Scalar[dtype]]): The memory area to fill.
  • low (Int): The inclusive lower bound.
  • high (Int): The inclusive upper bound.

def randint[dtype: DType](ptr: UnsafePointer[Scalar[dtype]], size: Int, low: Int, high: Int) where dtype.is_integral()

Fills memory with uniformly distributed random integers in the range [low, high].

Example:

from std.random import randint, seed
seed()
var size: Int = 10
var data = List(length=size, fill=Int32(0))
randint(data, -50, 50)
for i in range(size):
print(data[i]) # Random Int32 between -50 and 50

Constraints:

The type must be integral.

Parameters:

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

Args:

  • ptr (UnsafePointer[Scalar[dtype]]): The pointer to the memory area to fill.
  • size (Int): The number of elements to fill.
  • low (Int): The inclusive lower bound.
  • high (Int): The inclusive upper bound.