I'm working on a markov chain and have created a 2d hashmap that calculates the weighted probabilities. The output of this works fine.
I'm looking to find the best way to output the next value. The way I have it at the moment isn't working properly. Ent1.first
is the incoming midiNote
. Ent2.first
is the potential outgoing value and Ent2.second
is the weighted probability.
When midiNote
comes in, I need to look into the table and find the weighted probabilities and using rand()
pick the next value. One problem is that I only need this to happen once, not for every time in the for loop. Here is a snippet of my table calculation for simplicity's sake, but if you'd like me to post the entire code let me know.
void getCountTable(int midiNote) {
for(auto const &ent1: cdf) {
midiNote = ent1.first;
for (auto const &ent2: ent1.second) {
//console out all resulting note transition weights
//std::cout << "Note: " << ent1.first << std::endl <<"Next note: " << ent2.first <<std::endl << "Weight: " << ent2.second << std::endl << std::endl;
//TRYING TO FIGURE HOW TO HANDLE THIS. JUST WANT TO HAPPEN ONCE FOR EACH INCOMING VALUE
//psuedo-random values between 0-1
float r = static_cast <float> (rand()) / static_cast<float> (RAND_MAX);
//calculate next value
if (r < ent2.second) {
int output = ent2.first;
std::cout << ent1.first << " " << output << std::endl;
}
}
}
}