PHP mt_rand() Function

Definition and Usage

The 'mt' prefix in function's name stands for Mersenne Twister. The mt_rand() function returns an integer using Mersenne Twister Random Number Generator method. This function is a drop-in replacement for PHP's rand() function. default range is between 0 and platform specific mt_getrandmax(). On 64 bit Windows OS, it is 2147483647. The mt_rand() function can be called without arguments (in which case the default range will be used) or by specifying min and max parameters.

This function always returns an integer.

Syntax

mt_rand ( void ) : int
mt_rand ( int $min , int $max ) : int

Parameters

Sr.No Parameter & Description
1 min
lower limit of range to return a number from. Default is 0
2 max
Upper limit of range to return a number from. Default is mt_ getrandmax()

Return Values

PHP mt_rand() function returns an integer between min and max using Mersenne Twister Random Number Generator technique This function is four times faster than rand() function. Note that this is not .advised to be used for cryptographic purpose.

PHP Version

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

Example

<?php
   echo "mt_rand(10.5,50.95) = " . mt_rand(10.55, 50.95) . "
"; ?>

Output

This may produce following result−

mt_rand(10.5,50.95) = 31
Updated on: 2026-03-11T23:22:53+05:30

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements