gpt4 book ai didi

OMNET++ 中的 C++ 编码

转载 作者:行者123 更新时间:2023-11-30 05:18:40 25 4
gpt4 key购买 nike

谁能告诉我如何在 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 范围内的随机统一,除了它不能与端口相同。

关于OMNET++ 中的 C++ 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41544163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com