rand() can generate a pseudo-random integer in an inclusive range. It is suitable for ordinary non-security uses; use random_int() when the value must be unpredictable.
$number = rand(1, 10);
echo $number;
Start with this current pattern. The examples below are retained for additional variations, output and reference.
echo rand(); // Random Output 16513
echo rand(8,10); // Random Output starting from 8 to 10
Syntax
rand ( min,max);
min ,max : (Optional) Minimum and maximu value ( both inclusive ) of random integer to be generated ( both to be given ) <?Php
$no_of_digits=5;
$var='';
for($i=1; $i<=$no_of_digits; $i++){
$var .=rand(0,9);
}
echo "<br>Random Value = $var";
?>
Output is here
Random Value = 33369
$random_float = mt_rand() / mt_getrandmax();
echo $random_float;
mt_srand(123); // Seed the generator
echo mt_rand()."<br>"; // Output: Same number every time the script is run
echo mt_rand(); // Output: Same number every time the script is run
mt_srand(987);
echo mt_rand(); // Output will change with a different seed value
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| princy | 02-02-2012 |
| how to generate the random number in php | |