2017-01-09 2 views
1

könnte mir jemand zeigen, wie Sie diesen Code zu schreiben, in in einer anderen Weise in OMNeT ++C++ Coding in OMNeT ++

while (Outgate == Port) {

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

und die anderen:

Leere Txc15 :: forwardMessage (TicTocMsg15 * msg, int port)

{

// inkrementale Anzahl der T-Stücke.

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

}

+1

Was meinst du mit "anders"? Kürzer? Weniger fehleranfällig? Weniger wiederholend? Etwas anderes? – molbdnilo

Antwort

1

Dieser hat eine begrenzt Laufzeitkosten:

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

zu beachten, dass der gleichförmige Zufall aus dem Bereich von 0 bis gezogen wird n-2 (nicht n-1). Wenn outgate größer oder gleich port ist, erhöhen wir es um eins. Dies führt effektiv zu einer zufälligen Gleichförmigkeit im Bereich 0 ... n-1, außer dass es nicht dasselbe wie der Port sein kann.