Random\Randomizer
new Random\Randomizer(?Random\Engine $engine = null)
OOP randomness facade (PHP 8.2+): inject a seedable engine (e.g. Mt19937) at construction and every sequence from getInt/shuffleArray becomes exactly reproducible; without a seed it falls back to the cryptographically secure engine.
C C23
#include <stdint.h>
#include <stdio.h>
static uint32_t s = 42;
static uint32_t next(void) { // xorshift32: a tiny deterministic PRNG (libc rand() differs across platforms)
s ^= s << 13; s ^= s >> 17; s ^= s << 5;
return s;
}
static int rng(int lo, int hi) { // inclusive range via modulo (slight bias, fine for demos)
return lo + (int)(next() % (uint32_t)(hi - lo + 1));
}
int main(void) {
/* C has no seedable RNG in the standard: a hand-rolled xorshift32;
the same seed always replays the identical sequence */
s = 42;
int a[3] = { rng(1, 100), rng(1, 100), rng(1, 100) };
s = 42;
int b[3] = { rng(1, 100), rng(1, 100), rng(1, 100) };
printf("seeded reproducible: %s\n", (a[0]==b[0] && a[1]==b[1] && a[2]==b[2]) ? "true" : "false");
printf("sequence: %d,%d,%d\n", a[0], a[1], a[2]);
/* Fisher-Yates by hand; reseeding replays the identical permutation */
char letters[6] = "abcde";
s = 42;
for (int i = 4; i > 0; i--) {
int j = rng(0, i);
char t = letters[i]; letters[i] = letters[j]; letters[j] = t;
}
printf("shuffled: %s\n", letters);
return 0;
}
seeded reproducible: true sequence: 33,49,60 shuffled: bedac
C++ C++26
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main() {
// std::mt19937 is standardized: same seed replays the identical sequence everywhere;
// uniform_int_distribution's mapping is implementation-defined, so only derived facts print
std::mt19937 a(42), b(42);
std::uniform_int_distribution<int> dist(1, 100);
std::vector<int> seqA = {dist(a), dist(a), dist(a)};
std::vector<int> seqB = {dist(b), dist(b), dist(b)};
std::cout << "seeded reproducible: " << (seqA == seqB ? "true" : "false") << "\n";
std::mt19937 s1(42), s2(42);
std::vector<char> v1 = {'a', 'b', 'c', 'd', 'e'};
std::vector<char> v2 = v1;
std::shuffle(v1.begin(), v1.end(), s1);
std::shuffle(v2.begin(), v2.end(), s2);
std::cout << "shuffle reproducible: " << (v1 == v2 ? "true" : "false") << "\n";
return 0;
}
seeded reproducible: true shuffle reproducible: true
C# v11.0.100
// A seeded Random replays the identical sequence (compat algorithm is fixed)
var a = new Random(42);
var b = new Random(42);
var seqA = new List<int> { a.Next(1, 101), a.Next(1, 101), a.Next(1, 101) };
var seqB = new List<int> { b.Next(1, 101), b.Next(1, 101), b.Next(1, 101) };
Console.WriteLine($"seeded reproducible: {seqA.SequenceEqual(seqB)}");
Console.WriteLine($"sequence: {string.Join(",", seqA)}");
seeded reproducible: True sequence: 67,15,13
Go v1.27.4
package main
import (
"fmt"
"math/rand"
"strings"
)
func main() {
// A dedicated seeded Source makes every sequence reproducible
a := rand.New(rand.NewSource(42))
b := rand.New(rand.NewSource(42))
seqA := make([]int, 3)
seqB := make([]int, 3)
for i := range seqA {
seqA[i] = a.Intn(100) + 1
seqB[i] = b.Intn(100) + 1
}
fmt.Println("seeded reproducible:", fmt.Sprint(seqA) == fmt.Sprint(seqB))
parts := make([]string, len(seqA))
for i, v := range seqA {
parts[i] = fmt.Sprint(v)
}
fmt.Println("sequence:", strings.Join(parts, ","))
s1 := rand.New(rand.NewSource(42))
s2 := rand.New(rand.NewSource(42))
perm1 := s1.Perm(5)
perm2 := s2.Perm(5)
fmt.Println("perm reproducible:", fmt.Sprint(perm1) == fmt.Sprint(perm2))
fmt.Println("perm:", perm1)
}
seeded reproducible: true sequence: 6,88,69 perm reproducible: true perm: [0 1 3 4 2]
Java v27.0.1
import java.util.Random;
public class Main {
public static void main(String[] args) {
// java.util.Random is spec-stable: same seed replays the identical sequence
Random a = new Random(42);
Random b = new Random(42);
StringBuilder sa = new StringBuilder();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
if (i > 0) { sa.append(","); sb.append(","); }
sa.append(a.nextInt(100) + 1);
sb.append(b.nextInt(100) + 1);
}
System.out.println("seeded reproducible: " + sa.toString().equals(sb.toString()));
System.out.println("sequence: " + sa);
int[] perm1 = seededPerm();
int[] perm2 = seededPerm();
StringBuilder p1 = new StringBuilder();
StringBuilder p2 = new StringBuilder();
for (int i = 0; i < 5; i++) { p1.append(perm1[i]); p2.append(perm2[i]); }
System.out.println("perm reproducible: " + p1.toString().equals(p2.toString()));
System.out.println("perm: " + p1);
}
static int[] seededPerm() {
int[] idx = {0, 1, 2, 3, 4};
Random r = new Random(42);
for (int i = 4; i > 0; i--) {
int j = r.nextInt(i + 1);
int t = idx[i]; idx[i] = idx[j]; idx[j] = t;
}
return idx;
}
}
seeded reproducible: true sequence: 31,64,49 perm reproducible: true perm: 12340
JavaScript v26.13.1
// Math.random has no seed API; a tiny mulberry32 keeps the demo reproducible
let s = 42;
function rand() {
s |= 0; s = (s + 0x6d2b79f5) | 0;
let t = Math.imul(s ^ (s >>> 15), 1 | s);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
function rng(lo, hi) { // inclusive range
return lo + Math.floor(rand() * (hi - lo + 1));
}
const seqA = [rng(1, 100), rng(1, 100), rng(1, 100)];
s = 42;
const seqB = [rng(1, 100), rng(1, 100), rng(1, 100)];
console.log("seeded reproducible:", JSON.stringify(seqA) === JSON.stringify(seqB));
console.log("sequence:", seqA.join(","));
s = 42;
const letters = ["a", "b", "c", "d", "e"];
for (let i = 4; i > 0; i--) {
const j = rng(0, i);
[letters[i], letters[j]] = [letters[j], letters[i]];
}
console.log("shuffled:", letters.join(""));
seeded reproducible: true sequence: 61,45,86 shuffled: aecbd
PHP v8.6.0
// Seedable engines make randomness reproducible
$a = new Random\Randomizer(new Random\Engine\Mt19937(42));
$b = new Random\Randomizer(new Random\Engine\Mt19937(42));
$seqA = [$a->getInt(1, 100), $a->getInt(1, 100), $a->getInt(1, 100)];
$seqB = [$b->getInt(1, 100), $b->getInt(1, 100), $b->getInt(1, 100)];
echo "seeded reproducible: ", var_export($seqA === $seqB, true), "\n";
echo "sequence: ", implode(",", $seqA), "\n";
$s1 = new Random\Randomizer(new Random\Engine\Mt19937(42));
$s2 = new Random\Randomizer(new Random\Engine\Mt19937(42));
$sh1 = $s1->shuffleArray(["a", "b", "c", "d", "e"]);
$sh2 = $s2->shuffleArray(["a", "b", "c", "d", "e"]);
echo "shuffle reproducible: ", var_export($sh1 === $sh2, true), "\n";
echo "shuffled: ", implode("", $sh1), "\n";
$u1 = new Random\Randomizer();
$u2 = new Random\Randomizer();
echo "unseeded differs: ", var_export($u1->getInt(1, 1000000) !== $u2->getInt(1, 1000000), true), "\n";
seeded reproducible: true sequence: 43,68,77 shuffle reproducible: true shuffled: eabdc unseeded differs: true
Python v3.15.2
# Seedable generator instances make randomness reproducible
import random
a = random.Random(42)
b = random.Random(42)
seq_a = [a.randint(1, 100) for _ in range(3)]
seq_b = [b.randint(1, 100) for _ in range(3)]
print("seeded reproducible:", seq_a == seq_b)
print("sequence:", ",".join(map(str, seq_a)))
s1 = random.Random(42)
s2 = random.Random(42)
sh1 = s1.sample(["a", "b", "c", "d", "e"], 5)
sh2 = s2.sample(["a", "b", "c", "d", "e"], 5)
print("shuffle reproducible:", sh1 == sh2)
print("shuffled:", "".join(sh1))
u1 = random.Random()
u2 = random.Random()
print("unseeded differs:", u1.randint(1, 1000000) != u2.randint(1, 1000000))
seeded reproducible: True sequence: 82,15,4 shuffle reproducible: True shuffled: aecbd unseeded differs: True
Rust v1.99.1
use rand::prelude::*;
fn main() {
// rand crate: a seedable StdRng replays every sequence exactly
let mut a = StdRng::seed_from_u64(42);
let mut b = StdRng::seed_from_u64(42);
let seq_a: Vec<i32> = (0..3).map(|_| a.gen_range(1..=100)).collect();
let seq_b: Vec<i32> = (0..3).map(|_| b.gen_range(1..=100)).collect();
println!("seeded reproducible: {}", seq_a == seq_b);
let mut s1 = StdRng::seed_from_u64(42);
let mut s2 = StdRng::seed_from_u64(42);
let mut v1 = vec!["a", "b", "c", "d", "e"];
let mut v2 = v1.clone();
v1.shuffle(&mut s1);
v2.shuffle(&mut s2);
println!("shuffle reproducible: {}", v1 == v2);
// OsRng-backed generators are unseeded: two draws differ (derived fact)
let x: u64 = rand::thread_rng().gen();
let y: u64 = rand::thread_rng().gen();
println!("unseeded differs: {}", x != y);
}
seeded reproducible: true shuffle reproducible: true unseeded differs: true
TypeScript v26.13.1
// Math.random has no seed API; a tiny mulberry32 keeps the demo reproducible
let s: number = 42;
function rand(): number {
s |= 0; s = (s + 0x6d2b79f5) | 0;
let t = Math.imul(s ^ (s >>> 15), 1 | s);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
function rng(lo: number, hi: number): number { // inclusive range
return lo + Math.floor(rand() * (hi - lo + 1));
}
const seqA: number[] = [rng(1, 100), rng(1, 100), rng(1, 100)];
s = 42;
const seqB: number[] = [rng(1, 100), rng(1, 100), rng(1, 100)];
console.log("seeded reproducible:", JSON.stringify(seqA) === JSON.stringify(seqB));
console.log("sequence:", seqA.join(","));
s = 42;
const letters: string[] = ["a", "b", "c", "d", "e"];
for (let i = 4; i > 0; i--) {
const j = rng(0, i);
[letters[i], letters[j]] = [letters[j], letters[i]];
}
console.log("shuffled:", letters.join(""));
seeded reproducible: true sequence: 61,45,86 shuffled: aecbd
Language differences
PHP: Random\Randomizer (OOP randomness facade, PHP 8.2+) takes an Engine at construction — new Randomizer(new Mt19937(42)) makes the whole getInt/shuffleArray sequence exactly reproducible (sequence values preset from local PHP 8.6.0); the no-arg constructor uses the cryptographically secure Random\Engine\Secure, a different worldview from mt_rand's hidden global state.
Python: random.Random(seed) is the structural counterpart — an independent instance holding the seed explicitly; random.seed() seeds the module-level global instead.
Go: rand.New(rand.NewSource(seed)) holds the RNG explicitly (go1compat guarantees the fixed-seed stream is stable across versions); the global rand auto-seeds since Go 1.22 and is not reproducible.
Rust: rand crate StdRng::seed_from_u64 seeds explicitly; note the StdRng algorithm is not guaranteed across major rand versions, so only boolean derived facts are preset here.
Java: java.util.Random(seed) has its algorithm written into the spec — same seed, same sequence across JDK versions.
C#: new Random(seed) uses the compat algorithm (only the parameterless constructor changed source in .NET 6+), so seeded construction is reproducible.
C: the standard library has no seedable RNG (rand/srand are global and differ across libcs) — hand-rolled xorshift32 (A31 precedent), same seed always replays.
C++: std::mt19937 is standardized and replays across platforms; uniform_int_distribution mapping is implementation-defined, so only booleans print.
JavaScript/TypeScript: Math.random has no seed mechanism in the standard and cannot reproduce — the demo uses a hand-written mulberry32 seeded RNG.