pTutorial

New way to learn PHP function

PHP mt_rand() Function | PHP Tutorial | pTutorial

Syntax

mt_rand([min, max]);

Parameter Description
Min Integer or float (optional)
Max Integer or float (optional)

Definition

It Returns the better random number than rand function.

Usage

Mt_rand function is used to find the random number in between min and max.

.

Note: Both parameter of mt_rand() function are optional.

.

Note: Generates a random integer, better than rand().


Example of mt_rand function

<?php

echo mt_rand();


?>

According to the above statement mt_rand function generate any number because of starting point and ending point are not defined. It may be 1511084859 or 827747377.

More example of mt_rand function

<?php

$a = 50;

$b=100;

echo "Ten random numbers are:
"; for($i = 1; $i <= 10; $i++) { echo mt_rand($a,$b); echo "
"; } ?>

In this example starting and ending point has been defined so that number will be in between 50 to 100 like 60,50,99. But it cannot cross given limit.

Float example

<?php

$min=10.2;

$max=20.2;

echo mt_rand($min,$max);

?>

Output

11

You can pass an integer as well as floats but generated random number always in form of an integer.

QUOTES OF THE DAY

Everything flows, nothing stays still.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER