rand

rand(int $min, int $max): int

Generates a random integer within a given range.

C C23

C23
#include <stdio.h>
int main(void) {
    // deterministic PRNG: unsigned overflow wraps automatically, replicating the JS LCG
    unsigned int seed = 42;
    seed = seed * 1664525u + 1013904223u;
    printf("%u\n", seed % 100 + 1);
    return 0;
}
Output
74

C++ C++26

C++26
#include <cstdio>
int main() {
    // deterministic PRNG: unsigned overflow wraps automatically, replicating the JS LCG
    unsigned int seed = 42;
    seed = seed * 1664525u + 1013904223u;
    std::printf("%u\n", seed % 100 + 1);
    return 0;
}
Output
74

C# v11.0.100

v11.0.100
// deterministic PRNG: uint overflow wraps automatically, replicating the JS LCG
uint seed = 42;
seed = seed * 1664525u + 1013904223u;
Console.WriteLine(seed % 100 + 1);
Output
74

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	// fixed seed for deterministic output (math/rand is pseudo-random; use crypto/rand for security)
	r := rand.New(rand.NewSource(42))
	fmt.Print(r.Intn(100) + 1)
}
Output
6

Java v27.0.1

v27.0.1
class Main {
    public static void main(String[] args) {
        // fixed seed for deterministic output (corresponds to PHP mt_srand; use SecureRandom for security)
        java.util.Random rnd = new java.util.Random(42);
        System.out.print(rnd.nextInt(100) + 1);
    }
}
Output
31

JavaScript v26.13.1

v26.13.1
let seed = 42;
const next = () => (seed = (seed * 1664525 + 1013904223) >>> 0);
console.log(next() % 100 + 1);
Output
74

PHP v8.6.0

v8.6.0
mt_srand(42);
echo mt_rand(1, 100);
Output
43

Python v3.15.2

v3.15.2
import random

random.seed(42)
print(random.randint(1, 100))
Output
82

Rust v1.99.1

v1.99.1
fn main() {
    // no RNG in the stdlib; this is a deterministic LCG (fixed seed for easy comparison)
    let mut seed: u32 = 42;
    seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
    println!("{}", seed % 100 + 1);
}
Output
74

TypeScript v26.13.1

v26.13.1
// deterministic PRNG: LCG linear congruential generator (use Math.random() in real projects)
let seed: number = 42;
const next = (): number => (seed = (seed * 1664525 + 1013904223) >>> 0);
console.log(next() % 100 + 1);
Output
74

Language differences

C: the example uses a deterministic LCG; unsigned overflow wraps around.

Go: uses math/rand with a fixed seed for determinism (like PHP mt_srand; use crypto/rand in security contexts).

Java: uses java.util.Random with a fixed seed for determinism (like PHP mt_srand; use java.security.SecureRandom in security contexts).

JavaScript: the standard library has no seedable PRNG, so a custom LCG is used; use crypto in security contexts.

PHP: seeding mechanisms differ across languages; all examples use a fixed seed for deterministic output; for security-sensitive scenarios use cryptographic randomness via random_int.

Python: fixed seed for determinism; use secrets in security contexts.

Rust: the standard library has no random number generator; the example is a fixed-seed deterministic LCG so the output is comparable.