Home > Software engineering >  Random word pick based on the number of times that they were used
Random word pick based on the number of times that they were used

Time:05-04

I have a vector of strings like this one:

std::vector<string> words ={"word1", "word2", "word3", "word4"}; //and many more

The code randomise the vector ( in an auxiliary vector) and takes the first word from the auxiliary vector. To do this I use random_shuffle. So far, everything goes very well. But now, I would like to minimise repeated words. My idea is to save the number of times a word is used to in another vector of <int> and randomise the words based on some weights that depends on the number of times that a word is selected by the generator. The words used more times have a lower weight and, for instance, the words never used have maximum weight (1.0 maybe). Is there something in STL that can do that? If not, does someone can help me on this hard task? The task is:

  • At beginning all words have the same probability (same weight) of being picked and the random function picks any word with equal weight;
  • After first pick, one of the words will have lower probability because it was already picked once, so,the random function will pick a random word from the list, but the one already picked will have lower probability of being picked;
  • After some tries suppose that word1 was picked 4 times word2 3 times word3 3 times and word4 0 times. The word with lower weight is word1 and the word maximum weight will be word4. Based on the number of times that word was picked the random function will generate the weights and pick a word.

thanks in advance

CodePudding user response:

std::discrete_distribution might help, something like

std::random_device rd;
std::mt19937 gen(rd());

std::vector<std::string> words = {"word1", "word2", "word3", "word4"};
std::vector<std::size_t> weights = {10, 10, 10, 10};

for (int i = 0; i != 12;   i) {
    std::discrete_distribution<> d(weights.begin(), weights.end());
    
    const auto index = d(gen);
    std::cout << words[index] << std::endl;
    weights[index]--;
}

Demo

  • Related