php - Increase the chances of a record being selected in a random select -
php - Increase the chances of a record being selected in a random select -
when using order rand()
how can go giving more "weight" records on others chances of them beingness selected increase?
i found an extensive article on problem provides ordering solution. i'm very, very, far mastering probability, statistics, or sort of math, based on own testing seems pretty solid. uses same thought of "selection weight" column.. create sure default , values column nonzero.
where selection_weight > 0 ... order ( -log(1.0 - rand()) / selection_weight )
using better random-selection query mike posted, seek like:
select users.* ( select id users selection_weight > 0 order ( -log(1.0 - rand()) / selection_weight ) ) random_users bring together users on random_users.id = users.id
this query ran in 10 - 20 ms on 2,000-record table using testing. hope helps!
original answerif have integer column in table corresponds selection weight:
order rand() * selection_weight desc
this slow, however.. i'll maintain looking improve solution.
php mysql random
Comments
Post a Comment