PHP mt_srand() Function



Definition and Usage

Prefix 'mt' in function's name stands for Mersenne Twister. The mt_srand() function is used to seed the Mersenne Twister random number generaror. Seeding initializes the random number generator. Most random number generators need initial seeding. In PHP, use of mt_srand() function is optional as it is done automatically.

This function doen't have any return value.

Syntax

mt_srand ([ int $seed [, int $mode = MT_RAND_MT19937 ]] ) : void

Parameters

Sr.No Parameter & Description
1 seed
an integer to be used as seed. If not given, a random number is given
2 mode
Use one of the following constants to specify mode of implementation
MT_RAND_MT19937 uses fixed Mersenne Twister implementation
MT_RAND_PHP uses default implementation

Return Values

This function doesn't return any value.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

This example the random number generator is first initialized before employing rand() function−

<?php
   mt_srand(5);
   echo "mt_rand(1,100)=", mt_rand(1,100);
?>

Output

This may produce following result −

mt_rand(1,100)=12

Example

 Live Demo

Following example uses current timestamp to initialize random number generator−

<?php
   mt_srand(time());
   echo "mt_rand()=", mt_rand();
?>

Output

This may produce following result−

mt_rand()=548287992
Updated on: 2020-06-30T07:10:52+05:30

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements