Vellvj
Vellvj

Reputation: 53

Select random number from a specific list

How to get random number [10, 20, 30, 50] in MySQL.

I have tried using ROUND(RAND() * (50 - 10) + 10) but, this will generate numbers such as 11, 12, 24 which i don't want.

I wonder if this code will:

FLOOR((RAND([5,10,15,20,25,30,35,40]))

but how?

I want to set my quantity column to choose random 10,20,30, or 50. not 11, 12 or other possible number using ROUND(RAND() * (50-10) + 10).

Hope it is clear.

Upvotes: 4

Views: 683

Answers (3)

Salman Arshad
Salman Arshad

Reputation: 272406

The following will give you one of the numbers from a list:

SELECT ELT(FLOOR(RAND() * 4) + 1, 10, 20, 30, 50);

Here 4 is the size of list and one is added since ELT is one-based.

Upvotes: 3

Mhamad El Itawi
Mhamad El Itawi

Reputation: 254

If you want number that can be devised by 10 use Div and then multiply by 10

SELECT ((FLOOR(RAND()*(50-10+1))+10) DIV 10 ) * 10 ;

Upvotes: 1

Ram
Ram

Reputation: 84

try this it's working fine

ROUND(RAND() * (5-1))*10

Upvotes: 3

Related Questions