根据百分比划分结果

发布于 2024-10-05 09:14:06 字数 277 浏览 0 评论 0原文

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

每次发送转账消息时,80% 支付给银行 A,20% 支付给银行 B,以此类推。我们需要在我们的数据库(sql 2005)中构建它,以便如果百分比发生变化,我们就能够更改它。 我很难根据数据库中的百分比编写计算规范,计算何时将 80% 的消息发送到银行 A,何时将 20% 的消息发送到银行 B。我最初想到实现一个计数器来对消息进行计数,然后根据百分比进行计算,但不确定算法。你能帮忙吗?

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 % 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2024-10-12 09:14:07

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

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

上面的内容显然是硬编码的,并且仅针对两个银行进行了简化,但我认为您可以看到如何将其推广到 N具有 N 个百分比的银行:按百分比排序并按升序测试数组。

如果您确实需要精确说明(将四个发送到银行 A,第五个始终发送到银行 B),那么我认为您需要实施一个计数器。我心中最重要的问题是:

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

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

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()

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.

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:

  • If you have odd ratios that don't reduce nicely (say 51% and 49%), is it OK to send 51 to Bank A and then 49 to Bank B, or would you want to alternate sends up until the last?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文