I have the function mutateSequence that takes in three parameters. The parameter p is a value between 0 and 1, inclusive. I need two if statements, one that is entered with probability 4p/5 and another that is entered with probability p/5. How do I write the logic to make this happen?
Code:
void mutateSequence(vector<pair<string, string>> v, int k, double p)
{
for (int i = 0; i < k - 1; i )
{
string subjectSequence = v[i].second;
for (int j = 0; j < subjectSequence.length(); j )
{
// with probability 4p/5 replace the nucelotide randomly
if (//enter with probability of 4p/5)
{
//do something
}
if (//enter with probability of p/5)
{
//do something
}
}
}
}
I am expecting that the first if statement is entered with probability 4p/5 and the second if statement is entered with probability p/5
CodePudding user response:
There's a very straightforward way to do this in modern C . First we set it up:
#include <random>
std::random_device rd;
std::mt19937 gen(rd());
// p entered by user elsewhere
// give "true" 4p/5 of the time
std::bernoulli_distribution d1(4.0*p/5.0);
// give "true" 1p/5 of the time
std::bernoulli_distribution d2(1.0*p/5.0);
Then when we want to use it:
if (d1(gen)) {
// replace nucleotide with 4p/5 probability
} else {
// something else with 1 - 4p/5 probability
}
If instead, you want do one thing with probability 4p/5 and then, independently, another thing with probability 1p/5, that's also easily done:
if (d1(gen)) {
// replace nucleotide with 4p/5 probability
}
if (d2(gen)) {
// something else with 1p/5 probability
}
See bernoulli_distribution
for more detail.