Skip to main content

Module random

Module random 

Source
Expand description

Random numbers: PHP’s Mersenne Twister, the engine behind mt_rand(), rand(), array_rand() and shuffle().

Mt19937 is the generator ext/random keeps as the default engine. Seeded with Mt19937::new it produces exactly the sequence PHP produces after mt_srand($seed), so code that picks at random can be replayed against PHP: Mt19937::next_u32 is one engine output, Mt19937::range is mt_rand($min, $max) (and every other bounded draw of the default engine), and Mt19937::array_rand is array_rand() of one key from a list.

PHPRust
mt_srand($seed) (MT_RAND_MT19937)Mt19937::new
the engine seeded from the OS on first useMt19937::from_entropy
mt_rand($min, $max), php_mt_rand_range()Mt19937::range
array_rand($list)Mt19937::array_rand
use php_std::random::Mt19937;

let mut mt = Mt19937::new(42);
assert_eq!(mt.range(1, 6), 1);
mt_srand(42);
echo mt_rand(1, 6), "\n"; // 1

Structs§

Mt19937
PHP’s Mersenne Twister (Random\Engine\Mt19937 in MT_RAND_MT19937 mode, the default engine).