根据百分比划分结果 [英] Divide results based on percentage

查看:48
本文介绍了根据百分比划分结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的应用程序中的新功能编写空间.现在我们要做的是,每当客户以特定货币发送转账消息时,我们需要在一个或多个银行之间进行分配.

I am writing the spacifications for a new feature in our application. Now what we want to do is whenever a message for trasfers is sent by client in a specific currency we need to divide between one or more banks.

每次发送转账消息时,80% 到银行 A 和 20% 到银行 B 等.我们需要在我们的 db (sql 2005) 中构建它,以便我们能够更改百分比.根据我们在数据库中的百分比,我很难编写用于计算何时将 80% 的消息发送到银行 A 以及何时将 20% 发送到银行 b 的规范.我最初想实现一个计数器来计算消息,然后根据百分比进行计算,但不确定算法.你能帮忙吗?

80 % to bank A and 20% to bank B etc each time a transfer message is sent. We need to build this in our db (sql 2005) so that if the percentage change for us to be able to change that. I am having difficulty writing the spec for the calculation of when the 80% of messages will be sent to BANK A and when the 20% will be sent to bank b based on the perecentage we have in our db. I initially thought of implenting a counter that counts the messages and then doing a calculation based on the percentage but not sure of the algorythm. can you help?

推荐答案

如果您不需要保证结果,您可以简单地将路由基于随机数生成器:

If you don't need the results to be guaranteed, you could simply base the routing on a random number generator:

var r = rand() // assume it yields a decimal between 0.0 and 1.0
if (r < 0.2) sendToBankB()
else         sendToBankA()

以上显然是硬编码和简化的,仅适用于两家银行,但我认为您可以看到如何将其推广到具有 N 个百分比的 N 个银行:按百分比排序并按升序测试数组.

The above is obviously hard-coded and simplified for just two banks, but I think you could see how to generalize it for N banks with N percentages: sort by percentage and test the array in increasing order.

如果您确实需要准确无误(将四个发送到 A 银行,将第五个发送到 B 银行),那么我认为您需要实现一个计数器.我心中最重要的问题是:

If you do need to be exact about it (send four to Bank A and the fifth always to Bank B) then I think you will need to implement a counter. The most important question in my mind for this is:

  • 如果您的奇数比率不能很好地降低(比如 51% 和 49%),是否可以将 51 发送到 A 银行,然后 49 发送到 B 银行,或者您希望交替发送直到最后一次?

一般来说,首先要通过除以最大公约数来降低您的比率,然后然后要么按顺序向每个银行发送正确数量的消息,要么沿着相同的重复周期将所有结果混洗在一起.

In general, look to first reduce your ratios by dividing by the greatest common divisor, and then either send the right number of messages to each bank sequentially, or shuffle all the results together along the same repetition period.

这篇关于根据百分比划分结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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