I want to use this algorithm for choosing a random word from a string:
Choose the first word with propability 1:1,
choose the second word with propability 1:2,
choose the nth word with propability 1:n
where each choice overwrites the last choice.
I want to use std::mt19937 for the "randomness". (is there a better method?)
My function should get an inputstring with this format:
word1 word2
word3 word4 word5
word6
where the number of words per line is unspecified, and words are seperated from eachother by blank spaces or new lines.
So what to fill this function with?
std::string random_word_from_string(std::string input)
{
std::string random_word="";
std::string word="";
std::stringstream iss(input);
int x=1;
while (iss >> word)
{
// random_word=word with probability 1/x
x++;
}
return random_word;
}
If this question is already answered in c++, sorry, but I wasn't able to find it! But I am very thankful for getting a link to the duplicate.
Aucun commentaire:
Enregistrer un commentaire