1 unstable release
Uses new Rust 2024
| 0.1.0 | Mar 1, 2026 |
|---|
#1694 in Algorithms
10KB
150 lines
PhpMt
A Rust implementation of PHP's MT19937-based random number generator.
This implementation is bit-for-bit compatible with PHP 7.1+ for:
mt_srand(seed)mt_rand()mt_rand(min, max)
Notes:
- In PHP 7.1+,
rand()is an alias ofmt_rand(). - In PHP 7.1+,
srand(seed)is an alias ofmt_srand(seed). - This implementation matches the Zend Engine MT19937 algorithm.
Implementation details:
- 624-element MT19937 state array
- Exact Zend tempering constants
mt_rand()returns 31-bit output (next_u32() >> 1)mt_rand(min, max)uses integer rejection sampling (no float scaling)
This crate is intended for deterministic cross-language compatibility and reproducible test vectors.
Not cryptographically secure.
php_mt
Bit-for-bit compatible implementation of PHP 7.1+ MT19937 (mt_rand) in Rust.
This crate reproduces the exact output of:
mt_srand(seed)mt_rand()mt_rand(min, max)
for PHP 7.1 and newer.
It is intended for deterministic cross-language compatibility and reproducible test vectors — not for cryptographic use.
Features
- Exact Zend Engine MT19937 constants
- 624-element state array
- 31-bit output for
mt_rand()(matches PHP) - Integer rejection sampling for
mt_rand(min, max) - No floating point scaling
- No external dependencies
- Fully deterministic
Example
use php_mt::PhpMt;
let mut rng = PhpMt::new(1234);
assert_eq!(rng.mt_rand(), 411284887);
assert_eq!(rng.mt_rand_range(0, 100), 20);