Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public default <T> void randomlyPerform(Collection<T> collection, Consumer<T> action, int minimumAmount, int maximumAmount) {
- if (collection.size() < maximumAmount) {
- throw new RuntimeException("Cannot have greater maximum amount than collection size");
- }
- boolean[] toRun = new boolean[maximumAmount];
- while (IntStream.range(0, toRun.length).filter(i -> toRun[i]).count() >= minimumAmount) {
- IntStream.range(0, maximumAmount).forEach(i -> toRun[i] = random.nextBoolean());
- }
- int index = 0;
- int performed = 0;
- for (T element : collection) {
- if (performed == maximumAmount) {
- break;
- }
- if (toRun[index]) {
- action.accept(element);
- performed++;
- System.out.println("Performed for " + performed);
- }
- index++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement