基于权重服务器页 [英] Server page based on weightage

查看:106
本文介绍了基于权重服务器页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有如下页面及其服务的权重:

Suppose we have following pages and their serving weightage:

Page1    50%
Page2    20%
Page3    15%
Page4    15%

什么是一个简单的公式来服务基于其权重的网页?我使用的红宝石。

What would be a simple formula to serve pages based on their weightage? I am using Ruby.

编辑:

下面是我如何想的。

actual_pct = Weightage 
tot_hits = Total hits against the URL
served_pct = Served percentage for each page based on tot_hits
served_to_actual_pct  = served_pct x 100 / actual_pct

上面会给我与他们的 served_to_actual_pct 页的数组。让我们假设我将服务根据其创建时间戳否供应又或者多个页面具有相同的时候,页面 served_to_actual_pct 。基于这种假设,我可以进行排序在这个阵列上创建时间戳,然后在 served_to_actual_pct 。在这一点上的阵列中的第一页将要被服务的页面。

Above will give me an array of pages with their served_to_actual_pct. Lets suppose I would be serving the pages based on their create timestamp when no page is served yet or more than one pages have the same served_to_actual_pct. Based on this assumption I can sort this array on create timestamp and then on served_to_actual_pct. At this point the first page in the array would be the page to be served.

我将不胜AP preciate任何帮助,意见或微调以上。

I would greatly appreciate any help, comments or fine tuning to above.

推荐答案

你需要的是一个加权的样本:

What you need is a weighted sample:

class Hash
  def weighted_sample
    target = rand * self.values.reduce(:+)
    key, weight = self.detect{ |key, weight| target -= weight; target < 0 }
    return key
  end
end

只要你想与它们的重量值的哈希键添加多页。然后调用 your_hash.weighted_sample 。键数值越高会拿出较多。

Add as many pages as you want as the keys of a hash with their weights as the value. Then call your_hash.weighted_sample. Keys with higher values will come up more often.

这篇关于基于权重服务器页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆