class Crowbar::PRNG

Overview

High-performance, deterministic 64-bit pseudo-random number generator. Uses Xoshiro256++ seeded via SplitMix64 for uniform statistical properties, long period (2^256 - 1), and complete reproducibility across platforms.

Defined in:

crowbar/prng.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(seed : UInt64 = PRNG.default_seed) #

Class Method Detail

def self.default_seed : UInt64 #

Instance Method Detail

def choice(array : Array(T)) : T forall T #

Random element from array


def choice?(array : Array(T)) : T | Nil forall T #

def next_u32 : UInt32 #

Generate next 32-bit unsigned integer


def next_u64 : UInt64 #

Generate next raw 64-bit unsigned integer using Xoshiro256++


def rand(max : Int) : Int32 #

Random integer in [0, max)


def rand(range : Range(Int32, Int32)) : Int32 #

Random integer within inclusive range (e.g. 1..10) or exclusive range (e.g. 0...5)


def rand_bool(prob : Float64 = 0.5) : Bool #

Returns true with probability p (default 0.5)


def rand_float : Float64 #

Random Float64 in [0.0, 1.0)


def rand_log(max_power : Int32 = 14) : Int32 #

Log-scale random distribution (useful for repeats, burst lengths, and byte counts) Generates values from 1 up to 2^max_power with logarithmic bias towards smaller values


def sample(array : Array(T), count : Int32) : Array(T) forall T #

Reservoir sample or simple sample


def seed : UInt64 #

def seed=(new_seed : UInt64) #

def seek(offset : Int) : Nil #

Fast-forwards PRNG state by offset iterations


def shuffle(array : Array(T)) : Array(T) forall T #

Non-destructive shuffle


def shuffle!(array : Array(T)) : Array(T) forall T #

Shuffle array in-place (Fisher-Yates) and return it