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).
Random
struct Random[rounds: Int = 10]
A high-performance random number generator using the Philox algorithm.
The Philox algorithm is a counter-based random number generator designed for parallel computing. It provides high-quality random numbers with excellent statistical properties and works efficiently on both CPU and GPU.
Parameters
- rounds (
Int): Number of mixing rounds to perform. Higher values provide better statistical quality at the cost of performance. Default is 10.
Implemented traits
AnyType,
Copyable,
ImplicitlyDestructible,
Movable
Methods
__init__
def __init__(out self, *, seed: UInt64 = UInt64(67280421310721), subsequence: UInt64 = UInt64(0), offset: UInt64 = UInt64(0))
Initialize the random number generator.
Args:
step
def step(mut self) -> SIMD[DType.uint32, 4]
Generate 4 random 32-bit unsigned integers.
Returns:
SIMD[DType.uint32, 4]: SIMD vector containing 4 random 32-bit unsigned integers.
step_uniform
def step_uniform(mut self) -> SIMD[DType.float32, 4]
Generate 4 random floating point numbers uniformly distributed in [0,1).
Returns:
SIMD[DType.float32, 4]: SIMD vector containing 4 random float32 values in range [0,1).
step_uniform_unbiased
def step_uniform_unbiased(mut self) -> SIMD[DType.float32, 4]
Generate 4 uniform float32 values in (0, 1), unbiased.
Uses all 32 raw bits of each Philox output via the conversion
u = (raw + 0.5) / 2^32, which is fused into a single FMA. The
resulting range is (2^-33, 1 - 2^-33) — never exactly 0 or 1.
Compared to step_uniform, this uses one extra bit of randomness
and is bounded away from 0, which removes the need for a 1.0 - u
guard before log(u) in Box-Muller transforms.
Returns:
SIMD[DType.float32, 4]: SIMD vector containing 4 random float32 values in (0, 1).