I have a dataset of rows each with an 'odds' number between 1 and 100. I am looking to do it in the most efficient way possible. The odds do not necessarily add up to 100.
I have had a few ideas.
a) select the whole dataset and then add all the odds up and generate a random number between 1 and that number. Then loop through the dataset deducting the odds from the number until it is 0.
I was hoping to minimize the impact on the database so I considered if I could only select the rows I needed.
b)
SELECT * FROM table WHERE (100*RAND()) < odds
I considered LIMIT 0,1
but then if items have the same probability only one of the will be returned
alternatively take the whole dataset and pick a random one from there... but then the odds are effected as it becomes a random with odds and then a random without odds thus the odds become tilted in favour of the higher odds (even more so).
I guess I could order by odds
ASC then take the whole dataset and then with php take a random out of the rows with the same odds as the first record (the lowest).
seems like a clumsy solution.
Does anyone have a superior solution, if not which one of the above is best?