PHP rand() Function

PHP rand() Function is used to generate a random integer number. A rand() function is built-in function in PHP.

Syntax :

rand();

Or

rand(int $min, int $max);

Parameter,

$min : Optional. It is the lowest number. Default is 0.
$max : Optional. It is the highest number.

Return Values :

It returns integer numbers randomly between min (or 0) and max (or getrandmax()).

The rand() function is an alias of mt_rand(). The mt_rand() function generates random value and is 4 times faster than rand().

Let's see below example to understand php rand() Function in details.

Example :

<?php
echo "<br><br> Generates a random integer : ".rand();
echo "<br><br> Generates a random integer : ".mt_rand();
echo "<br><br> Generates a random integer between 1000 to 9999 : ".rand(1000,9999);
echo "<br><br> Generates a random integer between 0 to getrandmax() : ".rand(0,getrandmax());

?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

32903