A
A
Alexey Lebedev2016-08-10 17:40:12
SQL
Alexey Lebedev, 2016-08-10 17:40:12

How to select a table row with a random chance?

There is a selection from the table:
id, chance
1, 20
2, 30,
3, 10
4, 5
chance - the chance of selecting this id.
How to do it right?
I have an idea, process in C#
Take all the data, calculate the amount of chance. Throw out a random number 0-sum. Then take one line at a time until we reach a line that is greater than the number. This is what you need
But you want something more cool.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Optimus, 2016-08-10
Pyan @marrk2

Select a random number from 1 to 100
If the selected number is in the range 1-20 then the SQL query SELECT id=1
If the selected number is in the range 96-100 then the SQL query SELECT id=4
, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question