Random Numbers and Independent Replication

152 views
Skip to first unread message

abdelkader ralem

unread,
Apr 6, 2024, 8:42:17 AM4/6/24
to ns-3-users
Hi, 
I'm a little confused with the concepts of randomness and independent replication in the simulation.
Here is my case : i want to run simulation 20 times with different sender and receiver nodes.

RngSeedManager::SetSeed(3); // Changes seed from default of 1 to 3
RngSeedManager::SetRun(7); // Changes run number from default of 1 to 7
 
In the code above, a change to the seed and the run numbers has been made from their default values of 1 (the default seed and run set to 1 are the ones responsible for having the same results every time, i guess).

My Question :
01: What part of the simulation will these changes affect, i mean, what will be random in my simulation if i set these values without creating random variables.

02 - Because i have two different variables : sendernodeindex, receivernodeindex, how to choose different indexes when the simulation is run, i mean how to link the randomvariable with my indexes (500 nodes in total) so to get a random node every time i call GetInteger from the random variable.
 

abdelkader ralem

unread,
Apr 6, 2024, 10:47:04 AM4/6/24
to ns-3-users
Update on the Question :
Here is the code that works for me :

//Seed the random number generator of c++ with the current time
//This will be used to change the run number everytime we run the simulation
srand(time(NULL));

//Change the seed and the run for ns3
RngSeedManager::SetSeed(10);
RngSeedManager::SetRun (rand() % 19);
//Random Variable to be used to set different senders and receivers
auto x = CreateObject<UniformRandomVariable>();
x->SetAttribute("Min", DoubleValue(0));
x->SetAttribute("Max", DoubleValue(199));
//Randomly select a sender node index
uint32_t senderNodeIndex = round(x->GetValue());
std::cout << senderNodeIndex <<std::endl;
// Randomly select a receiver node index (ensuring it's not the same as the sender node)
uint32_t receiverNodeIndex;
do {
receiverNodeIndex = round(x->GetValue());
} while (receiverNodeIndex == senderNodeIndex);
std::cout << receiverNodeIndex <<std::endl;

I have another question as a followup to the first :
when we set a different seed or run number, is the randomness introduced in the simulation is because of the different models
that we use (by default) that implement random variables (example propagation models , channels, etc). how to check what random variables are
in use in the simulation.

Tom Henderson

unread,
Apr 6, 2024, 11:39:31 AM4/6/24
to ns-3-...@googlegroups.com
A few tips:

1) Do not set the seed to different values; only set the run number (read the ns-3 manual chapter about random variables)

2) If you use srand()/rand() you will not be able to reproduce any result, which may not be ideal for reproducibility.  Instead, if you want N independent replications, set the run number to N different integers, possibly using a for loop in a script.

3) To get a count of all nodes in the simulation:

NodeContainer::GetGlobal().GetN();

4) To get the "i-th" node of all of the N nodes:

Ptr<Node> n = NodeContainer::GetGlobal().Get(i);

5) To randomly select a node out of all of the nodes:

Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable>();
// optionally, you can call uv->SetStream(integer_value); here
uint32_t maxIndex = NodeContainer::GetGlobal().GetN() - 1;
Ptr<Node> n = NodeContainer::GetGlobal().Get(uv->GetInteger(0, maxIndex));

(note, the above is untested).
--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/58ce20ee-e436-4cf2-a7e7-9ecb49aed319n%40googlegroups.com.


Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages