PHP: Generating a random number between Min and Max
( 17 Articles)

In order to generate a random number within a given range, you can use the rand() or the mt_rand() functions.
Example:
<?php
$x = rand(10, 20);
$b = mt_rand(20, 30);
// Printing the output
echo $x;
echo '<br/>';
echo $b;
?>
Output (the output is not consistent will change each time you execute your code):
20
21
Note: mt_rand() is 4 times faster than rand(). You can find more information about this in the official docs.
Further reading:
- PHP array_push examples
- PHP array_filter() examples
- PHP array_slice examples
- WordPress: Passing Params to get_template_part()
- PHP: Merging Arrays with array_merge() and Spread Syntax
I have made every effort to ensure that every piece of code in this article works properly, but I may have made some mistakes or omissions. If so, please send me an email: [email protected] or leave a comment to report errors.
Subscribe
0 Comments