Generation of objects with a given value within a defined distribution

0

I'm having a hard time creating percentage-related goals.

What I want to do is a method that generates objects, all objects have only one attribute.

I need 20% of the objects to be generated with this attribute having a value of 1 and the others having a value of 2, but I need it in a way that in my% of% with values 1 and 2 are not necessarily positioned one side by side.

    
asked by anonymous 10.11.2014 / 23:00

1 answer

2

If your difficulty is creating objects, try this:

import java.util.ArrayList;

public class Porcentagem {
    private final int valor;

    public Porcentagem(int valor) {
        this.valor = valor;
    }

    public int getValor() {
        return valor;
    }

    public static void main(String[] args) {
        ArrayList<Porcentagem> q = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            q.add(new Porcentagem(i % 5 == 0 ? 1 : 2));
        }
    }
}

Basically, this algorithm repeatedly inserts an element with a value of 1 followed by four elements with a value of 2. Thus, it guarantees a deterministic order where the 1s will not be on each other's side. If this is not what you want, please explain it better.

Note: There is no class ArrayQueue in java. I only found ArrayList and ArrayDeque . I used ArrayList in the above code, but you can use whichever one you like best.

    
12.11.2014 / 01:00