108

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.

1

6 Answers 6

162

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
Sign up to request clarification or add additional context in comments.

14 Comments

how I use this code. I tried like this - FLOOR(RAND() * 401) + 100 as number, but not working
NOTE: I dont have a column named 'number' in my db. This must be dynamically generated column. is it posible?
@EdHeal Actually I think round() will give a non-uniform distribution.
Better store the result to a variable and use the variable if you have master slave replication. SET @r=FLOOR(RAND() * 401) + 100, then SELECT @r.
RAND(), UUID(), NOW() are indeterministic functions. The calling of such functions should be avoid from being written into the bin log for replication. For example. INSERT INTO t SET ID=UUID(); will cause the value of the ID fields to be different on master and slaves. Instead it needs to be written as SET @uuid:=UUID();, and then INSERT INTO t SET ID=@uuid;, then run them in a single transaction. This will be replication safe. This is a bit off topic for this question. It doesn't say your answer has any problem. :)
|
11

As RAND produces a number 0 <= v < 1.0 (see documentation) you need to use ROUND to ensure that you can get the upper bound (500 in this case) and the lower bound (100 in this case)

So to produce the range you need:

SELECT name, address, ROUND(100.0 + 400.0 * RAND()) AS random_number
FROM users

3 Comments

This coding working like this - SELECT ROUND(100.0 + 400.0 * RAND()) AS random_number, but now working with my query
random_number column must be randomly generated column along with my query.
This method will make the first and last number less likely.
5

Additional to this answer, create a function like

CREATE FUNCTION myrandom(
    pmin INTEGER,
    pmax INTEGER
)
RETURNS INTEGER(11)
DETERMINISTIC
NO SQL
SQL SECURITY DEFINER
BEGIN
  RETURN floor(pmin+RAND()*(pmax-pmin));
END; 

and call like

SELECT myrandom(100,300);

This gives you random number between 100 and 300

Comments

4

these both are working nicely:

select round(<maxNumber>*rand())

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

1 Comment

I mean, to be clear, the only difference is that, in the first, min is 0 and ROUND is doing the work of FLOOR with +1 * [from 0 to less than 1]. And the first dupes this earlier answer (but in a way that doesn't answer the OP) and the second dupes this one.
3

You could create a random number using FLOOR(RAND() * n) as randnum (n is an integer), however if you do not need the same random number to be repeated then you will have to somewhat store in a temp table. So you can check it against with where randnum not in (select * from temptable)...

Comments

0

This is correct formula to find integers from i to j where i <= R <= j

FLOOR(min+RAND()*(max-min))

2 Comments

This is wrong, it will never produce j (or max). It produces a number i <= R < j.
Should be: FLOOR(min+RAND()*(max-min+1))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.