How to Install RANDOM.H Packages in Ns2

To import Random.h_ packages in the Ns2 tool, follow the steps that we have listed below. We will provide you with customized research solutions and interesting research ideas and topics  that will catch the readers’ attention.

The random header, which is described in the random.h file, is in charge of producing random values that are utilized in traffic generation, mobility models, and event timing, among other networks simulation facets. To simulate real-world network behavior, methods to create random variables from various distributions, such as uniform, exponential, normal, and Poisson, are declared in the random.h file. Through its ability to replicate uncertain network situations, this randomness aids in the creation of variability and more realistic simulation scenarios. The functionality from random.h is accessed indirectly in the TCL script.

PRE-REQUISITES:

  1. Fresh installation of Ubuntu 16.04 LTS:

Screenshot:

Fresh installation of Ubuntu 16.04 LTS

2.NS-2.35 Installation:

Screenshot:

NS-2.35 Installation

Here, Percentage (%)  symbol Denotes the successful installation of the NS 2.35 Simulator.

Screenshot:

HEADER FILE VERIFICATION:

  1. Locate to the ns-2.35 folder:

Screenshot:

2.Create the Main.tcl file in the scratch folder:

Next we need to create the Main.tcl file by using text editor in the ns-2.35 folder.

Screenshot:

Create the Main.tcl file in the scratch folder

Next we need to paste the below code to the Main.tcl file and save the file in the ns-2.35 folder.

Code:

set ns [new Simulator]

set nf [open out.nam w]

$ns namtrace-all $nf

proc finish {} {

    global ns nf

    $ns flush-trace

    close $nf

    exec nam out.nam &

    exit 0}

set topo [new Topography]

$topo load_flatgrid 1000 1000

set rng [new RNG]                ;# Random number generator object & Create a random variable generator

$rng seed 0                      ;# Seed the RNG

set num_nodes 5

set nodes [list]

for {set i 0} {$i < $num_nodes} {incr i} {

    set n [$ns node]

    lappend nodes $n

    set xpos [expr {int([$rng uniform 0 1000])}]

    set ypos [expr {int([$rng uniform 0 1000])}]

    $n set X_ $xpos

    $n set Y_ $ypos

    $n set Z_ 0

    puts “Node $i placed at position ($xpos, $ypos)”}

for {set i 0} {$i < [expr {$num_nodes – 1}]} {incr i} {

    $ns duplex-link [lindex $nodes $i] [lindex $nodes [expr {$i + 1}]] 1Mb 10ms DropTail}

set udp0 [new Agent/UDP]

$ns attach-agent [lindex $nodes 0] $udp0

set null0 [new Agent/Null]

$ns attach-agent [lindex $nodes 4] $null0

$ns connect $udp0 $null0

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set rate_ 1Mb

$cbr attach-agent $udp0

$ns at 0.1 “$cbr start”

$ns at 4.0 “$cbr stop”

$ns at 5.0 “finish”

$ns run

Screenshot:

Create the Main.tcl file in the scratch folder

3.Open the Terminal:

Next, we need to launch the terminal by right clicking the mouse in the ns-2.35 location.

Screenshot:

Open the Terminal

Screenshot:

Open the Terminal

4.NS-2.35 Configuration && Building Process:

Next, we need to configure and build the ns-2.35 folder to make the Created files need to work in ns-2.35 configuration.

Command:      “./make”

Screenshot:

Screenshot:

NS-2.35 Configuration && Building Process

Screenshot:

5.Importing Random.h:

Here we imported the Random.h header file code indirectly accessed the Random class define a uniform(min, max) method, used in the Tcl script ($rng uniform 0 1000), calls a random number generation function defined in Random.h. This function generates a random floating-point number between min and max. in this example program. Here we highlighted the code line that highlighted which is internally accessed Random.h that we will show class file that used in this code via Tools folder.

Screenshot:

Importing Random.h

Screenshot:

Importing Random.h

Here we will show the header file by opening Random.h file to show the class or function imported from the Random.h in the example code. “set xpos [expr {int([$rng uniform 0 1000])}]” eventually instantiating the uniform() method from Random.h is being called, generating random positions for each node. Therefore, the random number generation functions in Random.h are responsible for providing the random values that position the nodes in the simulation space.

Screenshot:

Importing Random.h

6.Executing the Example Program for Packet Header:

Then we need to run the Example program for Packet Header to view output of the program.

Command:      “./ns Main.tcl”

Screenshot:

Executing the Example Program for Random Header

Here we shown the output of the example program by using Random.h.

Screenshot:

Executing the Example Program for Random Header

Screenshot:

Executing the Example Program for Random Header

Screenshot:

Executing the Example Program for Random Header

Here, we have shown the out.tr file for Output of the Communication of the Network done in the simulation.

Screenshot:

Executing the Example Program for Random Header

In the NS 2.35 Simulation Example Program, the Random.h Header file is successfully imported.

We’ve got all the right tools and resources to support you when you need it. Reach out for personalized research services from ns2projects.com. We’ll show you how to implement it in your projects with a thorough explanation. Just send us a message, and we’ll be happy to assist you further.