mardi 26 avril 2016

Filling a new vector by sampling without replacement from an old one

Is there a good and efficient algorithm in C++ for sampling without replacement that could easily be applied to the following function?

It takes two vectors, new and old, and fills the latter in a loop by repeatedly sampling from the former (rng.i0 is a random number generator function that I use to return a random integer between 0 and given value).

void diluationexpansionstep(std::vector<long> &oldpopulation,
                            std::vector<long> &newpopulation,
                            long newpopsize)
{
    for (int i = 1; i <= newpopsize;i++) {
        int index_a = rng.i0(oldpopulation.size());
        newpopulation.push_back(oldpopulation[index_a]);
    }
}

Aucun commentaire:

Enregistrer un commentaire