- All Implemented Interfaces:
- Distributor
public class CyclicDistributor
extends Object
implements Distributor
This class hands out numbers between the range ZERO and `max` so that the
first value is random and the rest values will not repeat.
The range is treated as the perimeter of a circle. Each value is a point in
this perimeter. The first point handed out is always random. The next values
are calculated in iterations or cycles. Each cycle has a 2^n number of values
to hand out. All the values are equidistant from the other values of the same
circle.
The iterations proceed until the quantity of values to be handed out in the
current iteration is greater than a half of the max value. When it happens,
the algorithm is restarted. Another random number is generated and handed
out. The following numbers will be calculated the same way as above.
Example:
Say the range is between ZERO and 360, similar to the 360 degrees of a
circle. The first handed out is random, and for coincidence it is ZERO. The
second value handed out is 180 degrees away from the first point. The third
is at 270 degrees far from the first one. The forth 90 degrees. And so on...
This algorithm is very simple, but is easier to understand watching it
running. There's an animation made with HTML canvas that shows the algorithm
in action. Each point drawn in the circle of the animation is like a value
been handed out. Each value is at the same distance of the others in the same
iteration or cycle.