What's RND / seed in c++?
<p>I'm trying to play with some code..keep getting an compile error <code>RND not declared in scope</code> I found a part of the code that defined it if it ran on linux and if defined it on windows thus ignoring Mac users(no biggie, I would ignore them too!).  I removed that part of the code and defined it using the linux settings(since I figured my Mac is more closer to linux than windows), but then I get the same error but for seed. The odd thing is those seed errors are at the same spot at the RND error was. So my question is what the heck is RND/Seed? My searches found them specific to VB but not sure if its useful since I'm using C++.  </p>

<p>Here's an offensive code snipped(viewers discretion is advised):</p>

<p><code>mi = (int)(round(RND*(dimc-1)));</code></p>

<p>Any tips/suggestions would be great. I'm just starting to learn about c++ so I maybe missing something very simple.</p>

<p>Here's the entire code(stole it from here <a href="http://cg.iit.bme.hu/~zsolnai/gfx/genetic/" rel="nofollow">http://cg.iit.bme.hu/~zsolnai/gfx/genetic/</a> ):</p>

<pre><code>// a fast genetic algorithm for the 0-1 knapsack problem
// by karoly zsolnai - keeroy@cs.bme.hu
// test case: 1000 items, 50 knapsack size
//
// compilation by: g++ genetic.cpp -O3 -ffast-math -fopenmp
#include &lt;math.h&gt;
#include &lt;time.h&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;limits.h&gt;


#define RND ((double)rand_r(&amp;seed)/RAND_MAX) // reentrant uniform rnd



using namespace std;

struct chromo {
    chromo(int dimc) { items = new bool[dimc]; }
    ~chromo() { items = NULL; }
    void mutate(const int dimc, const int count) {
        int mi;
        for(int i=0;i&lt;count;i++) {
            mi = (int)(round(RND*(dimc-1)));
            items[mi] = !items[mi];
        }
    }
    bool* items;
    int f;
};

int fitness(bool*&amp; x, const int dimc, const vector&lt;int&gt;&amp; v, const vector&lt;int&gt;&amp; w, const int limit) {
    int fit = 0, wsum = 0;
    for(int i=0;i&lt;dimc;i++) {
        wsum += x[i]*w[i];
        fit += x[i]*v[i];
    }
    if(wsum&gt;limit) fit -= 7*(wsum-limit); // penalty for invalid solutions
    return fit;
}

void crossover1p(const chromo&amp; c1, const chromo&amp; c2, const chromo&amp; c3, const int dimc, const int cp) {
    for(int i=0;i&lt;dimc;i++) {
        if(i&lt;cp) { c3.items[i] = c1.items[i]; }
        else { c3.items[i] = c2.items[i]; }
    }
}

void crossover1p_b(const chromo &amp;c1, const chromo &amp;c2, const chromo &amp;c3, int dimc, int cp) {
    for(int i=0;i&lt;dimc;i++) {
        if(i&gt;=cp) { c3.items[i] = c1.items[i]; }
        else { c3.items[i] = c2.items[i]; }
    }
}

void crossoverrand(const chromo &amp;c1, const chromo &amp;c2, const chromo &amp;c3, const int dimc) {
    for(int i=0;i&lt;dimc;i++) {
        if(round(RND)) { c3.items[i] = c1.items[i]; }
        else { c3.items[i] = c2.items[i]; }
    }
}

void crossoverarit(const chromo &amp;c1, const chromo &amp;c2, const chromo &amp;c3, int dimc) {
    for(int i=0;i&lt;dimc;i++) {
        c3.items[i] = (c1.items[i]^c2.items[i]);
    }
}

bool cfit(const chromo &amp;c1,const chromo &amp;c2) { return c1.f &gt; c2.f; }
bool cmpfun(const std::pair&lt;int,double&gt; &amp;r1, const std::pair&lt;int,double&gt; &amp;r2) { return r1.second &gt; r2.second; }

int coin(const double crp) { // a cointoss
    if(RND&lt;crp) return 1; // crossover
    else return 0; // mutation
}

// initializes the chromosomes with the results of a greedy algorithm
void initpopg(bool**&amp; c, const std::vector&lt;int&gt; &amp;w, const std::vector&lt;int&gt; &amp;v, const int dimw, const int limit, const int pop) {
    std::vector&lt;std::pair&lt;int,double&gt; &gt; rvals(dimw);
    std::vector&lt;int&gt; index(dimw,0);
    for(int i=0;i&lt;dimw;i++) {
        rvals.push_back(std::pair&lt;int,double&gt;(std::make_pair(i,(double)v[i]/(double)w[i])));
    }
    std::sort(rvals.begin(),rvals.end(),cmpfun);
    int currentw = 0, k;
    for(int i=0;i&lt;dimw;i++) {
        k = rvals[i].first;
        if(currentw + w[k] &lt;= limit) { // greedy fill
            currentw += w[k];
            index[k] = 1;
        }
    }
    for(int i=0;i&lt;pop;i++) {
        for(int j=0;j&lt;dimw;j++) {
            c[i][j] = index[j];
        }
    }
}

int main() {
    printf("\n");
    srand(time(NULL));
    vector&lt;int&gt; w, v; // items weights and values
    int info=0;
    FILE *f = fopen("1000_weights.txt","r");
    FILE *f2 = fopen("1000_values.txt","r");
    while(!feof(f) || !feof(f2) ) {
        fscanf(f," %d ",&amp;info);
        w.push_back(info);
        info=0;
        fscanf(f2," %d ",&amp;info);
        v.push_back(info);
    } // omitted fclose(f1) and fclose(f2) on purpose
    const int limit = 50; // knapsack weight limit
    const int pop = 250; // chromosome population size
    const int gens = INT_MAX; // maximum number of generations
    const int disc = (int)(ceil(pop*0.8)); // chromosomes discarded via elitism
    const int dimw = w.size();
    int best = 0, ind = 0, ind2 = 0; // a few helpers for the main()
    int parc = 0; // parent index for crossover
    double avg = 0, crp = 0.35; // crossover probability
    vector&lt;chromo&gt; ch(pop,chromo(dimw));
    bool **c = new bool*[pop];
    for(int i=0;i&lt;pop;i++) c[i] = new bool[dimw];
    clock_t start = clock();
    printf("Initializing population with a greedy algorithm...");
    initpopg(c,w,v,dimw,limit,pop);
    printf("done!");
    for(int i=0;i&lt;pop;i++) {
        ch[i].items = c[i];
        ch[i].f = fitness(ch[i].items, dimw ,v, w, limit);
    }
    printf("\n\n");

    for(int p=0;p&lt;gens;p++) {
        std::sort(ch.begin(), ch.end(), cfit);
        #pragma omp parallel for shared(ch)
        for(int i=0;i&lt;pop;i++) {
            if(i&gt;pop-disc) { // elitism - only processes the discarded chromosomes
                if(coin(crp)==1) { // crossover section
                    ind = parc+round(10*RND); // choosing parents for crossover
                    ind2 = parc+1+round(10*RND);
                    // choose a crossover strategy here
                    crossover1p(ch[ind%pop],ch[ind2%pop],ch[i],dimw,round(RND*(dimw-1)));
//                  crossoverrand(ch[ind],ch[ind2],ch[i],dimw);
//                  crossoverarit(ch[0],ch[1],ch[i],dimw);
                    ch[i].f = fitness(ch[i].items, dimw ,v, w, limit);
                    parc += 1;
                }
                else { // mutation section
                    ch[i].mutate(dimw,1);
                    ch[i].f = fitness(ch[i].items, dimw ,v, w, limit);
                }
            }
            avg += ch[i].f;
            if(ch[i].f&gt;best) best=ch[i].f;
        }
        parc = 0;
        if(p%5==0) {
            printf("\n#%d\t",p);
            printf("best fitness: %d \t",best);
            printf("avg fitness: %f",avg/pop);
            if(best == 675) goto end; // psst...don't tell anyone
        }
        best = avg = 0;
    }

end:
    printf("\n\n");
    clock_t end = clock();
    double t = (double)(end-start)/CLOCKS_PER_SEC;
    printf("\nCompletion time: %fs.\n",t);
    return 0;
}
</code></pre>
