c++ - 如何在 C++ 中重新启动循环(在随机运行中查找唯一序列)

标签 c++ loops sequence

以下代码尝试在 K 次运行中生成随机字符串。 但是我们希望新生成的字符串完全不同 及其引用字符串。

为此,我尝试使用“continue”来重新启动随机 字符串生成过程。但是它似乎不起作用。 我下面的方法有什么问题?

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <time.h>
using namespace std;


// In this code we want to print new string that is entirely different with  
// with those in initVector 


template <typename T> void  prn_vec(std::vector < T >&arg, string sep="")
{   // simple function for printing vector
    for (int n = 0; n < arg.size(); n++) {
        cout << arg[n] << sep; 
    }
}


int main  ( int arg_count, char *arg_vec[] ) {

    // This is reference string
    vector <string> initVec;
    initVec.push_back("A");
    initVec.push_back("A");
    initVec.push_back("A");
    initVec.push_back("A");

    vector <string> DNA;
      DNA.push_back("A");
      DNA.push_back("C");
      DNA.push_back("G");
      DNA.push_back("T");

    for (unsigned i =0; i< 10000; i++) {

       vector <string> newString;
       for(unsigned j=0; j<initVec.size(); j++) {

         int dnaNo = rand() % 4;
         string newBase = DNA[dnaNo];
         string oldBase = initVec[j];

         int sameCount = 0;
         if (newBase == oldBase) {
            sameCount++;
         }

         if (sameCount == initVec.size()) {
              continue;
         }

         newString.push_back(newBase);

       } 
       cout << "Run " << i << " : ";
       prn_vec<string>(newString);
       cout << endl;

    }

    return 0;
}

最佳答案

您的代码乍一看还不错,除非我遗漏了您的大部分要求。 阅读this在使用 rand() 之前。当然,continue 部分除外。您要做的是查看它是否与 initVector 相同,对吧?在将其推送或打印到控制台之前,可以进行简单的比较。

int sameCount = 0;
if (newBase == oldBase) {
 sameCount++;
}
// sameCount can be 1 at most, 0 otherwise
// this check never return true
if (sameCount == initVec.size()) {
continue;
}

sameCount 变量在您每次为 newString 创建新条目时初始化,并在 结束时超出范围for 循环。因此,它不会增加以作为对重复生成的正确检查。理想情况下,您应该使用 std::set 并继续插入其中。不允许重复,您可以避免很多麻烦。

关于使用 rand() srand() 和随机数生成的更多信息:

来自 comp.lang.c 常见问题解答:

[...]the low-order bits of many random number generators are distressingly non-random

如果你想让你的随机数保持在范围内

[0, 1, ... N - 1]

与简单的 rand() % N(如链接中所建议的)相比,更好的方法是使用以下方法:

(int)((double)rand() / ((double)RAND_MAX + 1) * N)

现在,如果您要运行您的程序,每次您都会得到相同的一组 10000 条奇数随机 DNA 链。原来这是因为:

It's a characteristic of most pseudo-random number generators (and a defined property of the C library rand) that they always start with the same number and go through the same sequence.

来自另一个FAQ comp.lang.c.

要在运行中获得不同的链,请尝试以下操作:

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;
    int main  ( int arg_count, char *arg_vec[] ) {

    // most pseudo-random number generators 
    // always start with the same number and 
    // go through the same sequence. 
    // coax it to do something different!
    srand((unsigned int)time((time_t *)NULL));

    // This is reference string
    string initVec("AAAA");    
    // the family
    string DNA("ACGT");

    for (unsigned i =0; i< 5; i++) {
       string newString;
       for(unsigned j=0; j<initVec.size(); j++) {
         int dnaNo = (int)((double)rand() / ((double)RAND_MAX + 1) * 4);
         char newBase = DNA[dnaNo];         
         newString += newBase;
       }
               // ideally push in a std::set 
               // for now keep displaying everything
         if (newString != initVec) {
               cout << "Run " << i << " : " << newString << endl; 
            }
         }
     return 0;
}

关于c++ - 如何在 C++ 中重新启动循环(在随机运行中查找唯一序列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/614012/

相关文章:

python - 比较列表中的每个元素是否都大于其他所有元素

mysql - 查找缺失的序列间隙mysql

Java 序列生成器服务

c++ - 简单的 C++ 代码不起作用

c++ - 图像矩的快速计算

c++ - xcode 6.x arm64 代码构建失败,没有任何编译或链接错误

c++ - 无法在头文件中声明 ifstream 类成员

javascript - 检索对象中嵌套的值

java - 创建一个循环,以便 ActionListener 在单击 Jbutton 时遍历图标列表