1

我正在编写一个使用特定位大小的 binaryNumber 对象和包含这些 binaryNumber 对象数组的对象的应用程序。

代码看起来有点正确,但是即使我尝试使用 mutate 函数进行 1000 次迭代,在某些情况下也不会翻转任何位......这很奇怪,因为根据正态分布,1 位的平均值不应该翻转?

我基本上想要做的是有一段代码来确定是否基于低概率翻转二进制字符串的单个位(例如,我想要一个 0.001 的概率 - 这是 1000 分之一 - 或 - 0.1% 的概率)

注意:必须单独检查每个位的概率(每个位应该有 0.1% 的机会被翻转/反转。

现在我的功能看起来像这样:提前谢谢。

void Organism::mutate()
{
    // this function forces chance mutation based on a predetermined probability: 0.001 chance, which is 0.1 % = 1 in a thousand
    // iterate through the bits, generate a random number between 0-1000 and if it is 1, then perform a mutation/flip.
    for (int i = 0; i < chromosomeLength; i++)
        for (int k = 0; k < chromosome[0].numBits; k++)
        {
            srand(time(NULL)); // seed based on time.
            //check the probability:
            bool doMut = (rand() % 1000) <= 1;
            if (doMut == 1) //if the generated number is within probability
            {
                chromosome[i][k] = !chromosome[i][k];
                cout << "A MUTATION OCCURED!!!" << endl;
                exit(EXIT_FAILURE);
            }

        }


}
4

0 回答 0