mt_srand(): Seeds the Mersenne Twister Random Number Generator

mt_srand(time());  
echo mt_rand();
Syntax
void mt_srand ([ int $seed [, int $mode = MT_RAND_MT19937 ]] )
ParameterDESCRIPTION
$seedOptional : Seed used
$moodOptional : MT_RAND_MT19937 or MT_RAND_PHP

Example 1: Seeding with Current Time

<?php
mt_srand(time());
echo mt_rand();  // Output: A pseudo-random number based on the current time as the seed
?>

Example 2: Using a Fixed Seed for Reproducibility

<?php
mt_srand(123);
echo mt_rand();  // Output: Always returns the same random number
?>

Example 3: Generating a Sequence of Random Numbers with a Seed

<?php
mt_srand(42);
for($i = 0; $i < 5; $i++) {
    echo mt_rand() . "<br>";
}
?>

Example 4: Resetting the Seed with Different Values

<?php
mt_srand(100);
echo mt_rand();  // Output: A specific pseudo-random number
mt_srand(200);
echo mt_rand();  // Output: A different specific pseudo-random number
?>

Example 5: Lottery Simulation with Seed

<?php
mt_srand(7);
$lottery_numbers = [];
for($i = 0; $i < 6; $i++) {
    $lottery_numbers[] = mt_rand(1, 49);
}
echo implode(", ", $lottery_numbers);  // Output: Random lottery numbers
?>

Math Functions getrandmax(): Getting highest random number max() function
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer