mt_rand
mt_rand(): int — mt_rand(int $min, int $max): int
Mersenne Twister pseudo-random integers: once mt_srand fixes the seed the sequence is fully reproducible — the only randomness entry whose output can be pre-set deterministically.
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// C rand() CAN be seeded, but the algorithm and RAND_MAX vary by libc
// (glibc/BSD/MSVC all differ) — only platform-independent DERIVED facts print
srand(42);
int a = rand();
int b = rand();
srand(42);
int a2 = rand();
int b2 = rand();
printf("%d %d\n", a == a2, b == b2); // same seed replays the identical sequence
printf("%d\n", a != b); // consecutive draws differ
printf("%d\n", a >= 0 && a <= RAND_MAX && b >= 0 && b <= RAND_MAX); // within range
return 0;
}
1 1 1 1
C++ C++26
#include <iostream>
#include <random>
int main() {
// std::mt19937 is fully specified by the standard: same seed, same sequence everywhere
std::mt19937 gen(42);
std::cout << gen() << "\n"; // raw 32-bit draw
std::cout << gen() << "\n";
std::mt19937 replay(42); // re-seeding replays the identical sequence
std::cout << replay() << "\n";
return 0;
}
1608637542 3421126067 1608637542
C# v11.0.100
// System.Random is a specified algorithm: a fixed seed replays the sequence
var r = new Random(42);
Console.WriteLine(r.Next()); // first draw
Console.WriteLine(r.Next(1, 101)); // bounded draw: 1..100 inclusive
var replay = new Random(42);
Console.WriteLine(replay.Next()); // identical after re-seeding
1434747710 15 1434747710
Go v1.27.4
package main
import (
"fmt"
"math/rand"
)
func main() {
// rand.NewSource keeps a documented deterministic sequence
r := rand.New(rand.NewSource(42))
fmt.Println(r.Int31()) // first draw
fmt.Println(r.Intn(100) + 1) // bounded draw: 1..100
replay := rand.New(rand.NewSource(42))
fmt.Println(replay.Int31()) // identical after re-seeding
}
801072305 88 801072305
Java v27.0.1
import java.util.Random;
public class Main {
public static void main(String[] args) {
// java.util.Random is specified: a fixed seed replays the sequence
Random r = new Random(42);
System.out.println(r.nextInt(Integer.MAX_VALUE)); // first draw
System.out.println(r.nextInt(100) + 1); // bounded draw: 1..100
Random replay = new Random(42);
System.out.println(replay.nextInt(Integer.MAX_VALUE)); // identical after re-seeding
}
}
1562431130 64 1562431130
JavaScript v26.13.1
// Math.random() CANNOT be seeded — by design, and its algorithm is unspecified.
// A minimal seeded PRNG (mulberry32) stands in to show seed-to-sequence:
function mulberry32(seed) {
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 next = mulberry32(42); // same seed -> same sequence on every run
console.log(next());
console.log(next());
0.6011037519201636 0.44829055899754167
PHP v8.6.0
// seed the Mersenne Twister: the whole sequence becomes reproducible
mt_srand(42);
echo mt_rand(), "\n"; // first draw
echo mt_rand(), "\n";
echo mt_rand(1, 100), "\n"; // bounded draw: 1..100 inclusive
echo mt_rand(1, 100), "\n";
mt_srand(42); // re-seeding replays the identical sequence
echo mt_rand(), "\n";
804318771 1710563033 77 15 804318771
Python v3.15.2
import random
# random module documents MT19937: a fixed seed replays the sequence
random.seed(42)
print(random.randint(0, 2**31 - 1)) # first draw
print(random.randint(1, 100)) # bounded draw
r = random.Random(42) # an isolated generator replays it too
print(r.randint(0, 2**31 - 1))
478163327 4 478163327
Rust v1.99.1
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
fn main() {
// StdRng::seed_from_u64: a fixed seed replays the sequence
// (rand crate 0.8; the StdRng algorithm may change across major versions)
let mut rng = StdRng::seed_from_u64(42);
println!("{}", rng.gen::<u32>()); // first draw
println!("{}", rng.gen_range(1..=100)); // bounded draw
let mut replay = StdRng::seed_from_u64(42);
println!("{}", replay.gen::<u32>()); // identical after re-seeding
}
572990626 53 572990626
TypeScript v26.13.1
// Same as JavaScript: Math.random() cannot be seeded, so hand-roll mulberry32
function mulberry32(seed: number): () => number {
let a = seed | 0;
return function (): number {
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 next: () => number = mulberry32(42); // same seed -> same sequence on every run
console.log(next());
console.log(next());
0.6011037519201636 0.44829055899754167
Language differences
PHP: after mt_srand(42) the mt_rand() sequence is fully fixed (output back-filled from a real local 8.6.0 run); the global MT19937 instance is shared with rand() and friends, and any call advances the same sequence; the 7.2 modulo-bias fix made same-seed sequences differ from 7.1.
A fixed seed does NOT mean the same sequence across languages: C++ mt19937, Java Random, C# Random, Python, Go and Rust are differently specified algorithms whose values disagree — the teaching point is reproducibility, not cross-language equality.
C: srand()/rand() exist, but the algorithm and RAND_MAX vary by libc (glibc/BSD/MSVC all differ), so the example prints only derived facts (same-seed replay, in-range) and pre-sets no concrete values.
C++: std::mt19937 is specified by the standard — same seed, same sequence on every platform; uniform_int_distribution mapping is implementation-defined, so the example prints raw 32-bit outputs.
JavaScript/TypeScript: Math.random() is unseedable by design and unspecified; the example hand-rolls mulberry32 to demonstrate seed-to-sequence.
Rust: StdRng::seed_from_u64 (rand crate pinned at 0.8), ChaCha12 underneath; a major rand upgrade may swap the algorithm and change the output — noted as-is.
Go: the rand.New(rand.NewSource(42)) sequence is documented as stable.
Security: mt_rand is not cryptographically secure (manual Caution); use random_int()/random_bytes() or Random\Randomizer for security purposes.