# PHP mt_rand() Function

PHPServer Side ProgrammingProgramming

## 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.NoParameter & Description
1min
lower limit of range to return a number from. Default is 0
2max
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

Live Demo

This example returns random number by calling mt_rand() without arguments−

<?php
echo "random number with no parameters mt_rand() = " . mt_rand() . "\n";
echo "another random number with no parameters mt_rand() = " . mt_rand() . "\n";
?>

## Output

This may produce following result (it being a random number, it is more likely to return different number every time)−

random number with no parameters mt_rand() = 173620951
another random number with no parameters mt_rand() = 749065865

## Example

Live Demo

Following example specifies min and max arguments for mt_rand() function−

<?php
echo "mt_rand(11,30) = " . mt_rand(11,30) . "\n";
echo "mt_rand(11,30) = " . mt_rand(11,30) . "\n";
?>

## Output

This may produce following result (it being a random number, it is more likely to return different number every time)−

mt_rand(11,30) = 24
mt_rand(11,30) = 14

## Example

Live Demo

Fractional part of float values of min and max parameters will be ignored−

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

## Output

This may produce following result−

mt_rand(10.5,50.95) = 31
Published on 30-Jun-2020 10:38:59