Is there a way to generate a random number based on a min and max?
For example, if min was 1 and max 20 it should generate any number between 1 and 20, including 1 and 20?
The rand() is an inbuilt function in PHP used to generate a random number ie., it can generate a random integer value in the range [min, max]. Syntax: rand(); The rand() function is used to generate a random integer.
The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.
The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.
Using rand() Function: The rand() function generates a pseudo random number between the given range or between 0 and the default max value (getgrandmax()) which depends on the system.
<?php $min=1; $max=20; echo rand($min,$max); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With