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;
}
dirkgentlys answer is pretty comprehensive for what I was trying to say now.
I'd like to recommend you don't use continue though, Most coding standards recommend against using continue for good reason as it makes flow control harder to follow.
Your algorithm is bogus. Whatever you are trying to do, you aren't doing it, and because there's not a single comment in there, I can't really tell where you went wrong.
Your inner loop:
Adding to that, your "newString" isn't a string at all, but a vector of strings.
So, your problem isn't even the use of
continue
, it's that your algorithm is FUBAR.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, thecontinue
part. What you are trying to do is see if this is the same as theinitVector
or not, right? A simple comparison would do before you push it in or print to the console.The
sameCount
variable is initialized each time you create a new entry to thenewString
and goes out of scope at the closing}
of thefor
loop. So, it will not be incremented to function as a proper check against duplicate generation. You should ideally, use astd::set
and 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:
continue
does not skip the incrementing part of thefor
loop. All it does is go directly to it, skipping the rest of the body of the loop.Is equivalent to:
No backslash in the
printf()
since I couldn't figure out how to make the text editor let me type one. :)Have you realized that sameCount never becomes more than 1? Since initVec.size() is greater than 1 execution never hits continue.
As others already said it is difficult to find out what was your intention with this code. Could you tell us please how do you mean "totally different" for example?