mercredi 25 mai 2016

Is this method of guaranteeing a fixed amount of random true values biased?

Assuming the random() function is random and returns a value in the range [0, 1]. Will the resulting true values be biased towards a direction in the array when using this method?

    int chances = 16;
    int guaranteed = 6;
    boolean[] result = new boolean[chances];
    for (int i = 0; i < result.length; i++) {
        if (random() <= (float) guaranteed / chances) {
            result[i] = true;
            guaranteed--;
        }
        chances--;
    }




Aucun commentaire:

Enregistrer un commentaire