Pezmc Posted January 17, 2007 Share Posted January 17, 2007 I have a mysql query querying my database using order by RAND() LIMIT 1 to sellect a random coice from the database, however I would like to have the chance of a object being chosen increased for certain values (Weighting?)In my database I have:[code]id name weight1 Pezm 12 Shog 1.13 Ebay 1.24 PLp 1.35 php 1.4[/code]So follwing the information in my database if a weighted random script is used php shuld be the most frequent with 1.4*the chance to get chosen, compared to Pezm,How can this be achieved? Link to comment https://forums.phpfreaks.com/topic/34620-weighted-randoms/ Share on other sites More sharing options...
utexas_pjm Posted January 17, 2007 Share Posted January 17, 2007 Try this:[code]SELECT *FROM `your_table` JOIN ( SELECT `id`, `weight` FROM `your_table` ORDER BY RAND() * ( 1 / `weight`) LIMIT 1 ) as `ref` USING (`id`)[/code]Best,Patrick Link to comment https://forums.phpfreaks.com/topic/34620-weighted-randoms/#findComment-163117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.