mt_srand / srand
mt_srand(?int $seed = null, int $mode = MT_RAND_MT19937): void
Seeds the global Mt19937 engine (srand is its alias since 7.1): a fixed seed gives a fully REPRODUCIBLE sequence; the algorithm is pinned since 7.1, so the numbers are stable across versions and platforms.
C C23
/* srand(3) seeds rand(3): the PAIR is the classic seeding API. Sequence
values are libc-specific (NOT portable), so only reproducibility - same
seed, same replay - is asserted across platforms. */
#include <stdio.h>
#include <stdlib.h>
static int draw(void) { return rand() % 100 + 1; }
int main(void) {
srand(7);
int a = draw(), b = draw(), c = draw();
srand(7); /* reseed: identical replay */
printf("%d\n", draw() == a);
printf("%d\n", draw() == b);
printf("%d\n", draw() == c);
return 0;
}
1 1 1
C++ C++26
// <random>: mt19937 seeds in its constructor (or via gen.seed(n)). The
// engine's raw output IS standardized - portable and reproducible.
#include <cstdint>
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(42);
std::uint32_t raw = gen(); // standardized: same on every platform
std::cout << raw << "\n";
gen.seed(42); // reseed: identical replay
std::cout << (gen() == raw) << "\n";
}
1608637542 1
C# v11.0.100
// new Random(seed) is the injected-engine form: same seed, same sequence.
var rng = new Random(42);
int a = rng.Next(1, 101), b = rng.Next(1, 101), c = rng.Next(1, 101);
Console.WriteLine($"{a},{b},{c}");
var replay = new Random(42); // same seed -> identical replay
Console.WriteLine($"{replay.Next(1, 101)},{replay.Next(1, 101)},{replay.Next(1, 101)}");
67,15,13 67,15,13
Go v1.27.4
// rand.New(rand.NewSource(seed)) is the injected-engine form (rand.Seed
// on the global source is deprecated since Go 1.20): same seed, same replay.
package main
import (
"fmt"
"math/rand"
)
func main() {
rng := rand.New(rand.NewSource(42))
fmt.Printf("%d,%d,%d\n", rng.Intn(100)+1, rng.Intn(100)+1, rng.Intn(100)+1)
replay := rand.New(rand.NewSource(42)) // same seed -> identical replay
fmt.Printf("%d,%d,%d\n", replay.Intn(100)+1, replay.Intn(100)+1, replay.Intn(100)+1)
}
6,88,69 6,88,69
Java v27.0.1
// new Random(seed) is the injected-engine form: same seed, same sequence.
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rng = new Random(42);
System.out.printf("%d,%d,%d%n", rng.nextInt(100) + 1, rng.nextInt(100) + 1, rng.nextInt(100) + 1);
Random replay = new Random(42); // same seed -> identical replay
System.out.printf("%d,%d,%d%n", replay.nextInt(100) + 1, replay.nextInt(100) + 1, replay.nextInt(100) + 1);
}
}
31,64,49 31,64,49
JavaScript v26.13.1
// Math.random CANNOT be seeded - the standard defines no seeding API.
// Seeding needs a hand-written generator (mulberry32 shown here).
function mulberry32(seed) { // tiny seedable PRNG
let a = seed >>> 0;
return function () {
a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const rng = mulberry32(42);
console.log(`${Math.floor(rng() * 100) + 1},${Math.floor(rng() * 100) + 1},${Math.floor(rng() * 100) + 1}`);
const replay = mulberry32(42); // same seed -> identical replay
console.log(`${Math.floor(replay() * 100) + 1},${Math.floor(replay() * 100) + 1},${Math.floor(replay() * 100) + 1}`);
61,45,86 61,45,86
PHP v8.6.0
// mt_srand seeds the global Mt19937 (srand is its alias since 7.1.0):
// seed -> fully REPRODUCIBLE sequence. PHP 7.1+ pins the algorithm, so
// these exact numbers are stable across versions and platforms.
mt_srand(42);
echo mt_rand(1, 100), ",", mt_rand(1, 100), ",", mt_rand(1, 100), "\n";
mt_srand(42); // same seed -> identical replay
echo mt_rand(1, 100), ",", mt_rand(1, 100), ",", mt_rand(1, 100), "\n";
srand(42); // alias of mt_srand: same global engine
echo mt_rand(1, 100), "\n";
43,68,77 43,68,77 43
Python v3.15.2
# random.seed() seeds the module-global Mersenne Twister - the direct
# mt_srand twin. Same seed -> identical replay (version-stable).
import random
random.seed(42)
print(f"{random.randint(1, 100)},{random.randint(1, 100)},{random.randint(1, 100)}")
random.seed(42) # same seed -> identical replay
print(f"{random.randint(1, 100)},{random.randint(1, 100)},{random.randint(1, 100)}")
82,15,4 82,15,4
Rust v1.99.1
// std has no RNG: a hand-written seedable engine (xorshift64) shows the
// seed -> sequence contract (real projects use rand::SeedableRng).
struct Xorshift64(u64);
impl Xorshift64 {
fn next(&mut self) -> u64 {
let mut x = self.0;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.0 = x;
x
}
}
fn main() {
let mut rng = Xorshift64(42);
let d = |r: &mut Xorshift64| (r.next() % 100) + 1;
println!("{},{},{}", d(&mut rng), d(&mut rng), d(&mut rng));
let mut replay = Xorshift64(42); // same seed -> identical replay
println!("{},{},{}", d(&mut replay), d(&mut replay), d(&mut replay));
}
75,72,55 75,72,55
TypeScript v26.13.1
// Same as JavaScript: Math.random is unseedable; mulberry32 demonstrates
// the seed -> sequence contract.
function mulberry32(seed: number): () => number {
let a = seed >>> 0;
return function () {
a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const rng: () => number = mulberry32(42);
const draw = (r: () => number): number => Math.floor(r() * 100) + 1;
console.log(`${draw(rng)},${draw(rng)},${draw(rng)}`);
const replay: () => number = mulberry32(42);
console.log(`${draw(replay)},${draw(replay)},${draw(replay)}`);
61,45,86 61,45,86
Language differences
PHP: the mt_rand sequence after mt_srand(42) is identical across versions and platforms (since 7.1: fixed correct Mt19937 + the default MT_RAND_MT19937 mode) - the 43,68,77 in this entry’s PHP block is a portable constant; srand is an alias of mt_srand since 7.1 (same global engine); seed is nullable since 8.3 (null = seeded from system entropy); the manual warns Mt19937 takes only a 32-bit seed, so the sequence space is 2^32 - by the birthday problem a ~10% collision arrives after only ~30k random seeds - unsuitable for key derivation; cryptographic use belongs to Random\Randomizer + a Secure engine (see the randomizer entry); ties into mt_rand (see the mt_rand entry): every determinism test for mt_rand depends on mt_srand pinning the seed. C: srand/rand sequences are libc-bound and cannot assert concrete values cross-platform - assert reproducibility only (exactly this entry’s approach). C++: std::mt19937 gen(42) raw output IS spelled out by the C++ standard (1608637542, verified in this entry) - the positive textbook case of a portable assertion; note that uniform_int_distribution mapping is NOT standardized - asserting it cross-implementation is the actual trap. Go: rand.New(rand.NewSource(42)) injects an engine (the global rand.Seed is deprecated since 1.20). Java/C#: new Random(42) injection. JavaScript/TypeScript: Math.random is unseedable - no standard API; mulberry32 demonstrates the seed contract. Python: random.seed() seeds the global Mersenne Twister - mentally aligned with mt_srand. Rust: no RNG in std - rand::SeedableRng is the ecosystem answer; this entry hand-writes xorshift64 for the contract. Teaching anchor: a seed is the reproducibility contract - fixed seeds for tests, system entropy in production; both disciplines are non-negotiable.