Sunday, September 27, 2009

[NS2] Deterministic v.s. Random

Deterministic Setting

By default, NS2 runs deterministic simulation. That is, you will get the same result for every run. The deterministic setting is usually good for debugging, where you would like to know what goes wrong at an exact point.

Random Setting

Statistical analysis involves running simulation for several times and find averages/variance for all the runs. NS2 is deterministic by default. The results from every run with default setting are the same, and the variance of the results is therefore zero. Statistical analysis require randomness so that the avarages and variances would be meaningful.

In order to introduce a randomness to NS2, you will need to insert the following line into the Tcl Simulation script prior to "$ns run":

$defaultRNG seed n

where is an integer. If n is zero, the simulation would be totally random. The result would be different for every run. If n is another integer, NS2 will use n as a seed for random number generator.

By default, NS2 sets n to "1". Therefore, every run generate the same result. If you set n to 101, by executing

$default seed 101

the result would be different from the result corresponding to the default setting. But all the result with n = 101 would still be the same.

For more information about Random Number Generators, see Chapter 12 in the following book from Springer:

T. Issaraiyakul and E. Hossain, "Introduction to Network Simulator
NS2", Springer 2008.

You may also find the following website useful:


Anonymous said...

This assumes that you are using the ns-2 facilities for getting the random numbers. If (for example) you are using c++ function rand(), then this won't solve it.

Teerawat Issariyakul said...

Yeah. This is good for NS2 only. In other languages, you may need different approaches.

Even, in NS2, this option works for Tcl only. If you would like to work on C++ part of NS2 you may need different approaches (which provide more functionality including the implementation of various random variables and/or multiple stream random number generators).

But for the beginner, putting "defaultRNG set n" would be an quick start to generate statistically independent results.

n0 identity said...

hi mr..Im fauzan,from indonesia..i want implemented Evolutionary Algorthm in NS 2.35 to optimized relay..could u teach me how to implemented in NS 2...i using OLSR..please helpme..

Teerawat Issariyakul said...

How much do you know about NS2? If you are new to NS2, I suggest you to read the following blog post.

In future correspondence, please be specific on your questions so that I can help you properly.

If you have NS2 book, you can refer to

for a quick startup. This is (in my opinion) the fastest way to start using NS2 properly.

Also, please read the following blog post, which is a guidance for asking right questions.

Ederval said...

Hi boss!!!!!
I am running a simulation and I am having the same result all the times. I change the seed, but is always the same: the results!!!
I put this line
global defaultRNG
$defaultRNG seed 0

I put your suggestion in the NS Ultimate site..but nothing works..the same results...every simulation I do and using AODV and DSR protocols. Could you give an idea..what is this happening??

Best regards!!!

Teerawat Issariyakul said...

@ Ederval

What you did is to set the seed to be zero. That's mean the simulation result would be the same every time.

What you should do is to change the seed to 0, 1, 2, 3 and so on. You can do this by

1st run...
$defaultRNG seed 0

2nd run...
$defaultRNG seed 1

3rd run...
$defaultRNG seed 2

4th run...
$defaultRNG seed 3

Me said...

Hi, I am working on some simulation in NS right now, trying to calculate the average throughput. No matter what seed I set ( I am passing this by command line arguments), I get the same value. Can you help me with where I might be going wrong?

Teerawat Issariyakul said...

You cannot passing the seed from the command line. You have to do it within the Tcl simulation script.

Quamar Niyaz said...

Hi Dr. Teerawat,
I am trying to add randomness in a wired network simulation by using $defaultRNG seed 0, $defaultRNG seed 1, and so on but every time I get the same trace file. However, when I did the same for wireless networks I got different traces for the same network topology. Kindly shed light into it.

Anonymous said...

Sir I'm trying to add mobility to UEs in an lte patched version of ns2, the usual setdest command is giving an error; do you know anything about it?

Unknown said...

how to randomly generate malicious node in AODV (using rand() function)?