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
rand()%(5*p) < 4*p
should do the trick – Dionnadionnerand()
is biased. Don't use it if you need any quality of randomness at all. It seems that you are looking forstd::bernoulli_distribution
. – Marcosp
a parameter for how big the probability should be or is it some random sample you are given to use for making the mutate/do-not-mutate selection? – Amplificationvoid mutateSequence(vector<pair<string, string>> v, ...
passesv
by value! Since you want to mutatev
, you probably want to pass it by reference instead, i.e.void mutateSequence(vector<pair<string, string>> &v, ...)
. – Characteristicallyp
exactly one of these two branches occurs, and given that something occurs, with probability 4/5 it is the first? – Zedif
, 1p/5 enter the secondif
, 1−p enter neither. The prototypical way to do this is to divide the line segment from 0 to 1 into three intervals of lengths 4p/5, 1p/5, and 1−p. Then draw a random number in [0, 1). If it is less than4*p/5
, it is in the first interval. Otherwise, if it is less thanp
, it is in the second interval (the interval from 4p/5 to p has length p/5). Otherwise, it is in the third interval. This can be done with a simple draw from a uniform distribution. I will let others answer on C++ features for this. – Amplificationelse if
rather thanif
, not sure – Consequential