C++ 在 OMNET ++ 中编码

C++ Coding in OMNET ++

本文关键字:编码 OMNET C++      更新时间:2023-10-16

谁能告诉我如何在OMNET++中以不同的方式编写这段代码:

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

另一个:

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

这个运行时成本有限:

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

请注意,均匀随机是从范围 0 到 n-2(而不是 n-1)绘制的。如果outgate大于或等于port,我们将其增加一。这有效地导致 0..n-1 范围内的随机均匀,只是它不能与端口相同。