Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number in range [min - max] using PHP

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?

like image 241
Val Avatar asked Nov 13 '10 17:11

Val


People also ask

How can I generate 5 random numbers in PHP?

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.

How do I generate a random number in PHP?

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.

What is Mt_rand function in PHP?

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.

Which function generates number between predefined minimum and maximum limit?

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.


1 Answers

<?php   $min=1;   $max=20;   echo rand($min,$max); ?> 
like image 152
Prisoner Avatar answered Oct 11 '22 11:10

Prisoner