Search code examples
mysqlsql

creating a random number using MYSQL


I would like to know is there a way to select randomly generated number between 100 and 500 along with a select query.

Eg: SELECT name, address, random_number FROM users

I dont have to store this number in db and only to use it to display purpose.

I tried it something like this, but it can't get to work..

SELECT name, address, FLOOR(RAND() * 500) AS random_number FROM users

Hope someone help me out.


Solution

  • This should give what you want:

    FLOOR(RAND() * 401) + 100
    

    Generically, FLOOR(RAND() * (<max> - <min> + 1)) + <min> generates a number between <min> and <max> inclusive.

    Update

    This full statement should work:

    SELECT name, address, FLOOR(RAND() * 401) + 100 AS `random_number` 
    FROM users