开发者

Divide results based on percentage

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 mess开发者_运维知识库ages 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()

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜