-3

H_ello lovely people,

my program is written as a scalable network framework. It consists of several components that run as individual programs. Additional instances of the individual components can be added dynamically.

The components initially register with IP and Port at a central unit. This manager periodically sends to the components where other components can be found. But not only that, each component is assigned a weight / probability / chance of how often it should be addressed compared to the others.

As an example: 1Master, Component A, B, C All Components registered at Master, Master sends to A: [B(127.0.0.1:8080, 3); C(127.0.0.1:8081. 5)]

A runs in a loop and calculates the communication partner over and over again from this data.

So, A should request B and C in a 3 to 5 ratio. How many requests each one ultimately gets depends on the running performance. This is about the ratio.

Of course, the numbers 3 and 5 come periodically and change dynamically. And it's not about 3 components but potentially hundreds.

My idea was:

Add 3 and 5. Calculate a random number between 1 and 8. If it is greater than 3, take C else B ....

But I think that's not a clean solution. Probably computationally intensive in every loop. In addition, management and data structures are expensive. In addition, I think that a random number from the STL is not balanced enough. Can someone perhaps give me a hint, how I implemented this cleanly or does someone have experiences with it or an idea?

Thank you in every case;)

  • *"I think that a random number from the STL is not balanced enough."*. There are several random generator, choose the one which match your need. – Jarod42 Nov 08 '17 at 13:35
  • Can you explain, how, your question, relates to C++? Since, apart from brief mention of STL, I can't see anything that would be C++ specific. Algorithms are language agnostic. – Algirdas Preidžius Nov 08 '17 at 13:35
  • BTW I mentioned several reasons that speak against my first idea, not just the RNG. Constructive ideas on how to implement such a scheduling professionally would help more. – Admiral Hemut Nov 08 '17 at 13:48
  • Why C ++? Of course, the algorithm is language-independent, but the info in which language the program was written harms nothing, as language features that are not known to me may help or deliver the solution. – Admiral Hemut Nov 08 '17 at 13:48
  • 1
    Read up on [Bresenham's line-drawing algorithm](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm). That's an identical problem couched in the language of a different domain. You have the advantage of working exclusively in the +,+ quadrant. – Toby Speight Nov 08 '17 at 16:13

1 Answers1

1

I have an idea for you: Why not try it with cummulative probabilities?

1.) Generate a uniformly distributed random number. 2.) Iterate through your list until the cumulative probability of the visited element is greater than the random number.

Look at this (Java code but will also work in C++), (your hint that you use C++ was very good!!!)

double p = Math.random();
double cumulativeProbability = 0.0;
for (Item item : items) {
    cumulativeProbability += item.probability();
    if (p <= cumulativeProbability) {
        return item;
    }
}
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
  • Thank you! That was a constructive contribution! That's what such a forum is about! Some can cut off a slice of it .... – Admiral Hemut Nov 08 '17 at 14:21