Random Number Range In MySQL

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.

1
2
3
<p>Rather than put in <em>(j-i)</em> 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.</p>
 
<pre language="sql}SELECT FLOOR(1 + (RAND() * 10));

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

 
Category: 

Share:

  • Add news feed
  • Bookmark this on Delicious

Add new comment