Complete reference for bash::framehead’s PRNG library including historical and modern pseudorandom number generators from middle square to xoshiro256
The random module provides a comprehensive collection of pseudorandom number generator (PRNG) algorithms, from historical curiosities to modern high-quality generators. Each algorithm is self-contained, educational, and operates on caller-supplied state with no hidden globals.
Security notice: None of these PRNGs are cryptographically secure. For security-sensitive applications, read from /dev/urandom directly.
Sebastiano Vigna, 2014 - Used in V8, SpiderMonkey, and WebKit Math.random().Quality: Good (passes most BigCrush tests) | Period: 2^128 - 1 | Use: General purpose
random::xorshiftr128plus(s0, s1)
Generate next value from xorshift128+ algorithm.Parameters:
s0 - State component 0 (64-bit)
s1 - State component 1 (64-bit)
Returns: “result s0_new s1_new” (space-separated)
s0=$(random::seed64)s1=$(random::seed64)read -r val s0 s1 <<< "$(random::xorshiftr128plus $s0 $s1)"echo "Random value: $val"# Generate more valuesread -r val s0 s1 <<< "$(random::xorshiftr128plus $s0 $s1)"
Note: Caller must unpack and pass updated state on next call.
Melissa O’Neill, 2014 - LCG with permutation output function.Quality: Excellent | Period: 2^64 | Use: General purpose, simulationPasses all known statistical tests.
random::pcg32(state, inc)
PCG32 with configurable increment.Parameters:
state - Current state (64-bit)
inc - Increment value (must be odd, enforced internally)
Returns: “result new_state” (space-separated)
state=$(random::seed64)inc=1442695040888963407 # Any odd numberread -r val state <<< "$(random::pcg32 $state $inc)"echo "Random: $val"# Next valueread -r val state <<< "$(random::pcg32 $state $inc)"
random::pcg32::fast(state)
PCG32 with hardcoded increment - Same quality, simpler usage.Parameters:
state - Current state (64-bit)
Returns: “result new_state”
state=$(random::seed64)read -r val state <<< "$(random::pcg32::fast $state)"echo "Random: $val"
Not considered cryptographically secure by modern standards but far stronger than other algorithms here. This is a simplified 8-word demonstration; full ISAAC uses 256-word state.
random::isaac::init(seed)
Initialize simplified ISAAC state.Parameters:
seed - Initial seed (64-bit)
Returns: “a b c s0 s1 s2 s3 s4 s5 s6 s7” (3 accumulators + 8 state words)
state_str=$(random::isaac::init $(random::seed64))read -r a b c s0 s1 s2 s3 s4 s5 s6 s7 <<< "$state_str"
random::isaac(a, b, c, s0, ..., s7)
Generate next value from simplified ISAAC.Parameters:
a, b, c - Internal accumulators
s0…s7 - 8 state words
Returns: “result new_a new_b new_c s0 … s7”
# Initializestate_str=$(random::isaac::init $(random::seed64))read -r a b c s0 s1 s2 s3 s4 s5 s6 s7 <<< "$state_str"# Generate valueresult=$(random::isaac $a $b $c $s0 $s1 $s2 $s3 $s4 $s5 $s6 $s7)read -r val a b c s0 s1 s2 s3 s4 s5 s6 s7 <<< "$result"echo "Random: $val"
#!/bin/bashsource random.sh# Using PCG32 (recommended for general use)state=$(random::seed64)for i in {1..10}; do read -r val state <<< "$(random::pcg32::fast $state)" echo "Random value $i: $val"done
#!/bin/bashsource random.sh# Same seed = same sequence (deterministic)fixed_seed=12345generate_sequence() { local state=$1 for i in {1..5}; do read -r val state <<< "$(random::pcg32::fast $state)" echo "$val" done}echo "First run:"generate_sequence $fixed_seedecho -e "\nSecond run (identical):"generate_sequence $fixed_seed
#!/bin/bashsource random.shsource timedate.shbenchmark_prng() { local name=$1 local iterations=10000 local start=$(timedate::time::stopwatch::start) local state=$(random::seed64) for (( i=0; i<iterations; i++ )); do read -r val state <<< "$(random::pcg32::fast $state)" done local elapsed=$(timedate::time::stopwatch::stop $start) echo "$name: $elapsed ms for $iterations iterations"}benchmark_prng "PCG32-Fast"