I have a data set, each of which has a number of "chances" from 1 to 100. I try to do this in the most efficient way. Odds are not necessarily up to 100.
I had a few ideas.
a) Select the entire data set, and then add all the coefficients and create a random number between 1 and that number. Then cycle through the data set by subtracting the coefficients from the number until 0.
I was hoping to minimize the impact on the database, so I wondered if I could only select the rows I needed.
b)
SELECT * FROM table WHERE (100*RAND()) < odds
I considered LIMIT 0,1
But if the elements have the same probability, then only one of them will be returned
As an alternative, take the entire dataset and select a random one ... but then the chances will be affected, because it becomes random with chances, and then random with no chances, so the chances are tilted in favor of higher odds (even more so).
I assume that order by oddsASC could take the entire data set, and then with PHP take a random one from the lines with the same chances as the first record (the lowest).
Sounds like clumsy solutions.
Does anyone have an excellent solution? If not, which of the above options is better?
source
share