1. <small id='L2CqG'></small><noframes id='L2CqG'>

  2. <legend id='L2CqG'><style id='L2CqG'><dir id='L2CqG'><q id='L2CqG'></q></dir></style></legend>

    <tfoot id='L2CqG'></tfoot>
    <i id='L2CqG'><tr id='L2CqG'><dt id='L2CqG'><q id='L2CqG'><span id='L2CqG'><b id='L2CqG'><form id='L2CqG'><ins id='L2CqG'></ins><ul id='L2CqG'></ul><sub id='L2CqG'></sub></form><legend id='L2CqG'></legend><bdo id='L2CqG'><pre id='L2CqG'><center id='L2CqG'></center></pre></bdo></b><th id='L2CqG'></th></span></q></dt></tr></i><div id='L2CqG'><tfoot id='L2CqG'></tfoot><dl id='L2CqG'><fieldset id='L2CqG'></fieldset></dl></div>
    • <bdo id='L2CqG'></bdo><ul id='L2CqG'></ul>

      如何考虑权重随机选择一行?

      时间:2023-06-01

      <small id='HyoMB'></small><noframes id='HyoMB'>

            <tfoot id='HyoMB'></tfoot>
            • <bdo id='HyoMB'></bdo><ul id='HyoMB'></ul>
            • <i id='HyoMB'><tr id='HyoMB'><dt id='HyoMB'><q id='HyoMB'><span id='HyoMB'><b id='HyoMB'><form id='HyoMB'><ins id='HyoMB'></ins><ul id='HyoMB'></ul><sub id='HyoMB'></sub></form><legend id='HyoMB'></legend><bdo id='HyoMB'><pre id='HyoMB'><center id='HyoMB'></center></pre></bdo></b><th id='HyoMB'></th></span></q></dt></tr></i><div id='HyoMB'><tfoot id='HyoMB'></tfoot><dl id='HyoMB'><fieldset id='HyoMB'></fieldset></dl></div>

                <tbody id='HyoMB'></tbody>
              <legend id='HyoMB'><style id='HyoMB'><dir id='HyoMB'><q id='HyoMB'></q></dir></style></legend>

              • 本文介绍了如何考虑权重随机选择一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一张看起来像这样的桌子:

                I have a table which looks like that:

                id: primary key
                content: varchar
                weight: int
                

                我想要做的是从这张表中随机选择一行,但要考虑到权重.例如,如果我有 3 行:

                What I want to do is randomly select one row from this table, but taking into account the weight. For example, if I have 3 rows:

                id, content, weight
                1, "some content", 60
                2, "other content", 40
                3, "something", 100
                

                第一行有 30% 的几率被选中,第二行有 20% 的几率被选中,第三行有 50% 的几率被选中.

                The first row has 30% chance of being selected, the second row has 20% chance of being selected, and the third row has 50% chance of being selected.

                有没有办法做到这一点?如果我必须执行 2 或 3 个查询,那不是问题.

                Is there a way to do that? If I have to execute 2 or 3 queries it's not a problem.

                推荐答案

                我觉得最简单的其实就是使用加权水库采样:

                I think the simplest is actually to use the weighted reservoir sampling:

                SELECT
                  id,
                  -LOG(RAND()) / weight AS priority
                FROM
                  your_table
                ORDER BY priority
                LIMIT 1;
                

                这是一种很棒的方法,可以让您从 N 个元素中选择 M 个,其中每个元素被选择的概率与其权重成正比.当您只需要一个元素时,它也能正常工作.这篇文章中描述了该方法.注意,他们选择了 POW(RAND(), 1/weight) 的最大值,相当于选择了 -LOG(RAND())/weight 的最小值.

                It's a great method that lets you choose M out of N elements where the probability to be chosen for each element is proportional to its weight. It works just as well when you happen to only want one element. The method is described in this article. Note that they choose the biggest values of POW(RAND(), 1/weight), which is equivalent to choosing the smallest values of -LOG(RAND()) / weight.

                这篇关于如何考虑权重随机选择一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:表是“只读"的 下一篇:MySQL动态交叉表

                相关文章

                • <bdo id='ujfAV'></bdo><ul id='ujfAV'></ul>

                    <small id='ujfAV'></small><noframes id='ujfAV'>

                    <tfoot id='ujfAV'></tfoot>
                  1. <legend id='ujfAV'><style id='ujfAV'><dir id='ujfAV'><q id='ujfAV'></q></dir></style></legend>
                    <i id='ujfAV'><tr id='ujfAV'><dt id='ujfAV'><q id='ujfAV'><span id='ujfAV'><b id='ujfAV'><form id='ujfAV'><ins id='ujfAV'></ins><ul id='ujfAV'></ul><sub id='ujfAV'></sub></form><legend id='ujfAV'></legend><bdo id='ujfAV'><pre id='ujfAV'><center id='ujfAV'></center></pre></bdo></b><th id='ujfAV'></th></span></q></dt></tr></i><div id='ujfAV'><tfoot id='ujfAV'></tfoot><dl id='ujfAV'><fieldset id='ujfAV'></fieldset></dl></div>