Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class RandomBucket
- {
- private final Random random;
- private final int min;
- private final int max;
- private final List<Integer> bucket;
- public RandomBucket(final int max)
- {
- this(0, max);
- }
- public RandomBucket(final int min, final int max)
- {
- this.random = new Random();
- this.min = min;
- this.max = max;
- this.bucket = new ArrayList<Integer>();
- this.fillIfNeeded();
- }
- private void fillIfNeeded()
- {
- if (this.bucket.size() != 0)
- return;
- for (int i = this.min; i < this.max; i++)
- this.bucket.add(i);
- }
- public int next()
- {
- this.fillIfNeeded();
- final int index = this.random.nextInt(this.bucket.size());
- return this.bucket.remove(index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement