Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Vein {
- private int sum;
- private List<Vein> subveins;
- private String name;
- private int weight;
- public Vein(String name, int weight) {
- this.name = Objects.requireNonNull(name);
- this.weight = Math.max(weight, 1);
- this.sum = 0;
- this.subveins = new ArrayList<>();
- }
- public void addSubVein(Vein other) {
- subveins.add(Objects.requireNonNull(other))
- sum+= other.getWeight();
- }
- pubic Vein selectVein() {
- if (subveins.isEmpty())
- return this;
- int random = Math.floor(Math.random()*sum);
- int curSum = 0;
- int i = 0;
- for (; i < subveins.size() && curSum < random; ++i) {
- curSum+=subveins.get(i).getWeight();
- }
- return subveins.get(Math.min(i, subveins.size()-1));
- }
- pubic int getWeight() {
- return weight;
- }
- public String getName() {
- return name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement