mt_srand()

mt_srand() — Seeds the Mersenne Twister Random Number Generator.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Seeds the random number generator with seed or with a random value if no seed is given.

Signature

mt_srand(?int $seed = null, int $mode = MT_RAND_MT19937): void

Parameters

ParameterTypeWhat it takes
$seed?intFills the state with values generated with a linear congruential generator that was seeded with seed interpreted as an unsigned 32 bit integer.
$modeintUse one of the following constants to specify the implementation of the algorithm to use. MT_RAND_MT19937: The correct Mt19937 implementation, available as of PHP 7.1.0. MT_RAND_PHP Uses an incorrect Mersenne Twister implementation which was used as the default up till PHP 7.1.0. This mode is available for backward compatibility.

Return value

No value is returned.

Example

The PHP manual ships no example for mt_srand(); the signature and the parameter table above are the contract.

Changelog

VersionChange
8.3.0seed is now nullable.
7.1.0srand() has been made an alias of mt_srand().
7.1.0mt_rand() has been updated to use the fixed, correct, version of the Mersenne Twister algorithm. To fall back to the old behaviour, use mt_srand() with MT_RAND_PHP as the second parameter.

See also

mt_rand(), mt_getrandmax(), srand()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page mt_srand() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.