'Random Number Generation problem in OMNeT++

I tried to generate two random integers j and k using the OMNET++ function intrand() but the first variable j takes a number out of the range. I had the same problem on OMNeT++6.0pre11 (installed on Ubuntu 18.04) and I thought that it was related to the software misbehavior. Then, I upgraded my OS to Ubuntu 20.04 and installed the OMNeT++6.0pre15 but I'm still experiencing the same issue.

Below are my piece of code and the output:

In my .h

public:

struct clusteringStruct {
   int CH_Index;
   map<int,double> Members;
   double FitnessValue;
   int trial = 0;
   double proba;
   double X[];
};

void generateNewSol(int n,clusteringStruct Cluster[],int z);

In my .cc

int D = 2;
clusteringStruct Cluster[5];

// Initialization...
for(t = 0; t < 5; ++t) {
   Cluster[t].X[0] = 0.5; // CH Residual Energy
   Cluster[t].X[1] = 500*sqrt(2); // Max distance
}

// Execution...
for(int i = 0; i < 5; i++) {
   generateNewSol(i,Cluster,D);
}

void generateNewSol(int i,clusteringStruct Cluster[],int D) {
//  Randomly select the variable j that is to be changed
    int j  = intrand(D);

//  Randomly select the neighbour k and ensure that he is different from i
    int k = intrand(5);
    while(k == i) {
        k = intrand(5);
    }

    clusteringStruct sol = Cluster[i];
    for(int q = 0; q < D; q++) {
      sol.X[q] = Cluster[i].X[q];
    } 

EV_DEBUG <<" i = "<<i<<" || j = "<<j<<" || k = "<<k<<endl;
}

The Output of j and k variable is per the image below :

enter image description here

Thanks in advance for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source