Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 138545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:16:52+00:00 2026-05-11T07:16:52+00:00

The following codes try to generate random strings over K runs. But we want

  • 0

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; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:16:53+00:00Added an answer on May 11, 2026 at 7:16 am

    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, the continue part. What you are trying to do is see if this is the same as the initVector or not, right? A simple comparison would do before you push it in or print to the console.

    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; } 

    The sameCount variable is initialized each time you create a new entry to the newString and goes out of scope at the closing } of the for loop. So, it will not be incremented to function as a proper check against duplicate generation. You should ideally, use a std::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:

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

    If you want to keep your randome numbers in the range

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

    a better method compared to the simple rand() % N (as advised in the link) is to use the following:

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

    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:

    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.

    from another FAQ of comp.lang.c.

    To get different strands across runs try the following:

    #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; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I try to create a function to generate select functions. But the following
The following code will generate a link to the page I want to get
I'm using the following code to try to read the results of a df
When I try to run the following code (from the REPL) in Clojure: (dotimes
I have the following code in Visual Studio 2005. Dim OutFile As System.IO.StreamWriter Try
We try to convert from string to Byte[] using the following Java code: String
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
When I try the following lookup in my code: Context initCtx = new InitialContext();
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.