mt_srand(time());
echo mt_rand();
Syntax
void mt_srand ([ int $seed [, int $mode = MT_RAND_MT19937 ]] )
| Parameter | DESCRIPTION |
|---|---|
| $seed | Optional : Seed used |
| $mood | Optional : MT_RAND_MT19937 or MT_RAND_PHP |
<?php
mt_srand(time());
echo mt_rand(); // Output: A pseudo-random number based on the current time as the seed
?>
<?php
mt_srand(123);
echo mt_rand(); // Output: Always returns the same random number
?>
<?php
mt_srand(42);
for($i = 0; $i < 5; $i++) {
echo mt_rand() . "<br>";
}
?>
<?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
?>
<?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
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.