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).
NormalRandom
struct NormalRandom[rounds: Int = 10]
A high-performance random number generator using the Box-Muller transform.
The Box-Muller transform is a method for generating pairs of independent standard normal random variables. Works efficiently on both CPU and GPU.
Parameters
- rounds (
Int): Number of mixing rounds to perform for the underlying random uniform generator that serves as input to the Box-Muller transform. 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))
Initializes the normal distribution random number generator.
Args:
step_normal
def step_normal(mut self, mean: Float32 = 0, stddev: Float32 = 1) -> SIMD[DType.float32, 8]
Generate 8 normally distributed random numbers using Box-Muller transform.
Args:
- mean (
Float32): Mean of the normal distribution. - stddev (
Float32): Standard deviation of the normal distribution.
Returns:
SIMD[DType.float32, 8]: SIMD vector containing 8 random float32 values from a normal distribution with mean mean and standard deviation stddev.
step_normal_4
def step_normal_4(mut self, mean: Float32 = 0, stddev: Float32 = 1) -> SIMD[DType.float32, 4]
Generate 4 normal floats from a single Philox step.
Pairs adjacent uint32 outputs (raw[0], raw[1]) and
(raw[2], raw[3]) into Box-Muller pairs (one Philox step total,
vs two for step_normal). Uses the unbiased
(raw + 0.5) / 2^32 uniform conversion, which is bounded away
from 0 — no 1.0 - u guard before log(u) is needed.
Result lane order: (sin(v_0)*s_0, cos(v_0)*s_0, sin(v_1)*s_1, cos(v_1)*s_1) — sin/cos interleaved per pair, matching the
natural output of a single Box-Muller call.
Args:
- mean (
Float32): Mean of the normal distribution. - stddev (
Float32): Standard deviation of the normal distribution.
Returns:
SIMD[DType.float32, 4]: SIMD vector of 4 normal float32 values.