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?
If you don’t need the results to be guaranteed, you could simply base the routing on a random number generator:
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:
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.