Skip to main content
Log in

Mojo struct

Random

struct Random[rounds: Int = 6]

A high-performance random number generator using the Philox algorithm.

The Philox algorithm is a counter-based random number generator designed for parallel and GPU computing. It provides high-quality random numbers with excellent statistical properties.

Parameters

  • rounds (Int): Number of mixing rounds to perform. Higher values provide better statistical quality at the cost of performance. Default is 6.

Implemented traits

AnyType, UnknownDestructibility

Methods

__init__

__init__(out self, *, seed: SIMD[uint64, 1] = __init__[__mlir_type.!pop.int_literal](0), subsequence: SIMD[uint64, 1] = __init__[__mlir_type.!pop.int_literal](0), offset: SIMD[uint64, 1] = __init__[__mlir_type.!pop.int_literal](0))

Initialize the random number generator.

Args:

  • seed (SIMD[uint64, 1]): Initial seed value for reproducible sequences. Default is 0.
  • subsequence (SIMD[uint64, 1]): Subsequence number for generating independent streams. Default is 0.
  • offset (SIMD[uint64, 1]): Starting offset in the sequence. Default is 0.

step

step(mut self) -> SIMD[uint32, 4]

Generate 4 random 32-bit unsigned integers.

Returns:

SIMD vector containing 4 random 32-bit unsigned integers.

step_uniform

step_uniform(mut self) -> SIMD[float32, 4]

Generate 4 random floating point numbers uniformly distributed in [0,1).

Returns:

SIMD vector containing 4 random float32 values in range [0,1).