The following codes try to generate random strings over K runs. But we want the newly generated strings to be totally different with its reference string.
For that I tried to use ‘continue’ to restart the random string generation process. However it doesn’t seem to work. What’s wrong with my approach below?
#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; }
Your code looks fine on first glance, unless I am missing a big part of your requirements. Read this before you use
rand(). Except of course, thecontinuepart. What you are trying to do is see if this is the same as theinitVectoror not, right? A simple comparison would do before you push it in or print to the console.The
sameCountvariable is initialized each time you create a new entry to thenewStringand goes out of scope at the closing}of theforloop. So, it will not be incremented to function as a proper check against duplicate generation. You should ideally, use astd::setand keep inserting in it. Duplicates are not allowed and you are saved from a lot of trouble.More on using
rand()srand()and random number generation:From the comp.lang.c FAQ:
If you want to keep your randome numbers in the range
a better method compared to the simple
rand() % N(as advised in the link) is to use the following:Now, if you were to run your program, every time you will get the same set of 10000 odd random DNA strands. Turns out this is because:
from another FAQ of comp.lang.c.
To get different strands across runs try the following: