Get a random number between two given numbers in MySQL

The following example shows how to generate a random number between two provided numbers. This may be useful in situations when the random number must fall between a certain minimum and maximum value.

The formula for this case is as shown below:

SELECT RAND()*(MaximumValue-MinimumValue)+MinimumValue;

Example:
SELECT FLOOR( RAND() * (1800-100) + 100) as RandomValue;

Remember, the output will differ with each run.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s