Moslem
Moslem

Reputation: 39

C++ Coding in OMNET ++

Could anybody show me how to write this code in in a different Way in OMNET++ :

while(outGate==port){             
    outGate = intuniform(0, n-1);
}

And the other one :

void Txc15::forwardMessage (TicTocMsg15 *msg, int port)
{
// Increment hop count.
msg->setHopCount(msg->getHopCount()+1);
int outGate, n = gateSize("gate");

if(port != -1){
    //we enter here only if the message is forwarded
    outGate=port;
    //checking for more than one gate!
    if (n>1)
    {
        /**
       * It will exit from the while cycle only if the intuniform function
       * will choose a port different from the incoming one.
       */
        while(outGate==port){

            outGate = intuniform(0, n-1);
        }
    }
    EV << "Forwarding message " << msg << " on gate[" << outGate << "]\n";
    //forward the message provided following the conditions.
    send(msg, "gate$o", outGate);
}else{
    //port is equal to -1 if and only if the message in newly generated
    outGate = intuniform(0, n-1); // Randomly choose a gate.
    EV << "Forwarding message " << msg << " on gate[" << outGate << "]\n";
    send(msg, "gate$o", outGate);
} 
}

Upvotes: 1

Views: 231

Answers (1)

Rudi
Rudi

Reputation: 6681

This one has limited runtime cost:

outGate = intuniform(0, n-2);
if (outgate >= port) outgate++;

note that the uniform random is drawn from the range 0 to n-2 (not n-1). If outgate is greater or equal than port, we increase it by one. This effectively results in a random uniform in range 0..n-1 except that it cannot be the same as port.

Upvotes: 1

Related Questions