Answer the question
In order to leave comments, you need to log in
How to split a stream of values by percentage?
Good evening!
Essence of the question:
there are constantly incoming values, you need to write them down as a percentage in 3 arrays. Percentages can be changed.
For example:
50% - 20% - 30%
, respectively, goes half of all to the first array, 1/5 to the second array and 30% to the third array.
The difficulty is that we do not know the final number of values and we need to somehow distribute it on the go.
Answer the question
In order to leave comments, you need to log in
Deterministic method - precision to a constant:
Let, the probability of hitting - 40%, 30%, 30%. A message has arrived at the distributor, it has a personal number (at least inside the distributor), if the remainder of dividing by 10 is less than or equal to 3, it goes to the first stream., from 4 to 7 - the second stream, from 7 to 9 - to the third. Need accuracy down to a percentage? We divide the number by 100, not by 10, and so on. Need high accuracy and more uniform loading? It's easy, A+B+C=100%, we express the probabilities B and C through A and some delta like this A+(A+d1)+(A+d2)=100%, Let's assume we have the same 10 messages. First, d1 messages will go to thread 2, then A messages will go to each thread in turn, and finally d2 messages will go to thread 3. And the counting starts all over again. You can optimize even more, but think it over yourself or hire a person who will spend time on this for your money.
The most interesting, in my opinion, but its accuracy with a small number of messages will be very low. We take a uniform pseudo-random generator and turn it into a given probability generator (if you don’t figure out how, this question was asked many times on the toaster, and there are probably ready-made libraries), we set the probabilities and “roll the dice” with each new message. Where the generator points, the message will go there. He has no problems with loading channels with a large number of messages.
random(100) <= 50 - first queue
50 < random(100) <=70 - second queue
70 < random(100) - third queue
I have three friends. I want to give one person 50% of the amount of money, another 30%, and the third 20%.
The amount of money I want to distribute is unknown.
How much money to give each friend?
Does this problem have a solution?
Accumulated 10 - divided 5 - 2 -3
So you repeat until you get the last ones.
If the remainder is less than 10, divide as a percentage.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question