根据受欢迎程度选择项目:避免美化排序
我有一个网站,用户可以在其中发布建议并对建议进行投票。在起始页面上,我最初列出了 10 个建议,标头每 7 秒获取一个新的随机建议。
我希望投票能够影响建议出现的概率,无论是在 10 条建议列表中还是在标题建议中。为此,我有一个小算法来计算受欢迎程度,考虑到选票、年龄和其他一些因素(需要大量调整)。
不管怎样,运行算法后,我有一个建议和流行度指数的字典,按流行度排序:
{ S = Suggestion1, P = 0.86 }
{ S = Suggestion2, P = 0.643 }
{ S = Suggestion3, P = 0.134 }
{ S = Suggestion4, P = 0.07 }
{ S = Suggestion5, P = 0.0 }
{ . . .}
我不希望这是一种美化的排序,所以我想在选择过程中引入一些随机元素。
简而言之,我希望流行度是从列表中选择建议的概率。
有了完整的建议/受欢迎程度列表,我该如何根据概率选出 10 个呢?如何将相同的内容应用于循环标题建议?
I have a site where users can post and vote on suggestions. On the from page I initially list 10 suggestions and the header fetches a new random suggestion every 7 seconds.
I want the votes to influence the probability a suggestion will show up, both on the 10-suggestion list and in the header-suggestion. To that end I have a small algorithm to calculate popularity, taking into account votes, age and a couple other things (needs lots of tweaking).
Anyway, after running the algorithm I have a dictionary of suggestions and popularity index, sorted by popularity:
{ S = Suggestion1, P = 0.86 }
{ S = Suggestion2, P = 0.643 }
{ S = Suggestion3, P = 0.134 }
{ S = Suggestion4, P = 0.07 }
{ S = Suggestion5, P = 0.0 }
{ . . .}
I don't want this to be a glorified sort, so I'd like to introduce some random element to the selection process.
In short, I'd like the popularity to be the probability a suggestion gets picked out of the list.
Having a full list of suggestion/popularity, how do I go about picking 10 out based on probabilities? How can I apply the same to the looping header suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕我不知道如何快速执行此操作,但如果内存中有集合,您可以这样做:
请注意,您不需要对列表进行排序即可使该算法发挥作用。
如果列表是静态的,您可以构建范围并进行一些二进制搜索,但如果列表不断变化,那么我不知道更好的方法。
这是一个示例 LINQPad 程序,演示:
输出:
I'm afraid I don't know how to do this very fast, but if you have the collection in memory you can do it like this:
Note that you do not need to sort the list for this algorithm to work.
If the list is static, you could build ranges and do some binary searches, but if the list keeps changing, then I don't know a better way.
Here is a sample LINQPad program that demonstrates:
Output: