Random Number Range In MySQL

5th April 2008 - 2 minutes read time

To create a number between one value and the next you can use the following formula, where i is the lower end of the range and j is the higher end of the range.

FLOOR(i + RAND() * (j – i))

Rather than put in (j-i) in your query you should put in the result. So for a number between 1 and 10 you would make i = 1 and j = 11. 11-1 = 10 so the query would run like this.

SELECT FLOOR(1 + (RAND() * 10));

For a number between 64 and 104 you would use the following query.

SELECT FLOOR(64 + (RAND() * 41));


This is brilliant. I wrote 30 lines to accomplish this but never thought of the 'Floor Plan' so to speak.

Dr. Dey (Mon, 06/16/2014 - 23:58)

Add new comment

The content of this field is kept private and will not be shown publicly.