"Go" ns2 random number

Source: Internet
Author: User
Tags tcl code

Random number generator (Random number Generator)

[ preface ]

Random number generator is very important in NS2, the random number generator is used when the location of network node in Nam or when the application starts transmitting or ending data transmission. Therefore, in this section, I will show you how to use the random number generator in NS2.

[ seed ] and distribution (distribution)]

The numbers generated by the random number generator are controlled by seeds and distributions, and different seeds or distributions produce different random numbers. We can think of this, when a seed and distribution decision, the random number generator will produce a table, the table is composed of a long series of different numbers, when we need a random number, the random number generator will choose the first number in this table, when another random number is needed, The random number generator picks up the second number in the table, and so on, so that when the seed and distribution are used, the random numbers will be the same, and if not at the same time, the resulting random number will be different. In NS2, if the value of the seed is 0, then each time the program is executed, the random number generator will produce a different table, that is, each time the random number obtained is not the same.

[Creating Random Variable]

If we want to generate a random number generator in NS2 and set the seed to 2, we can put the following program code into TCL code.

Set RNG [New RNG]

$RNG Seed 2

when the random variable is generated, the next thing to decide is the distribution that we can use Pareto,Constant,Uniform,exponential, or Hyperexponentail and so on distribution.

1. Pareto Distribution: To provide expectation and shaper parameterβ.

Set R1 [New Randomvariable/pareto]

$r 1 use-rng $rng

$r 1 Set Avg_ 10.0

$r 1 Set Shape_ 1.2

2. Constant:

Set R2 [New Randomvariable/constant]

$r 2 use-rng $rng

$r 2 Set Avg_ 5.0

3. Uniform Distribution: To provide minimum and maximum values

Set R3 [New Randomvariable/uniform]

$r 3 use-rng $rng

$r 3 Set Min_ 0.0

$r 3 Set Max_ 10.0

4. exponential distribution: To provide an average

Set r4 [New Randomvariable/exponential]

$r 4 use-rng $rng

$r 4 Set Avg_ 5

5. hyperexponential Distribution:

Set R5 [New Randomvariable/hyperexponential]

$r 5 use-rng $rng

$r 5 Set Avg_ 1.0

$r 5 Set COV_ 4.0

[Tcl Code]

Set RNG [New RNG]

$RNG Seed 2

Puts "Testing Pareto distribution"

Set R1 [New Randomvariable/pareto]

$r 1 use-rng $rng

$r 1 Set Avg_ 10.0

$r 1 Set Shape_ 1.2

For {set I 1} {$i <=3} {incr i} {

puts [$r 1 value]

}

Puts "Testing Constant distribution"

Set R2 [New Randomvariable/constant]

$r 2 use-rng $rng

$r 2 Set Avg_ 5.0

For {set I 1} {$i <=3} {incr i} {

puts [$r 2 value]

}

Puts "Testing Uniform distribution"

Set R3 [New Randomvariable/uniform]

$r 3 use-rng $rng

$r 3 Set Min_ 0.0

$r 3 Set Max_ 10.0

For {set I 1} {$i <=3} {incr i} {

puts [$r 3 value]

}

Puts "testing exponential distribution"

Set r4 [New Randomvariable/exponential]

$r 4 use-rng $rng

$r 4 Set Avg_ 5

For {set I 1} {$i <=3} {incr i} {

puts [$r 4 value]

}

Puts "Testing hyperexponential distribution"

Set R5 [New Randomvariable/hyperexponential]

$r 5 use-rng $rng

$r 5 Set Avg_ 1.0

$r 5 Set COV_ 4.0

For {set I 1} {$i <=3} {incr i} {

puts [$r 5 value]

}

[ execution result ]

Testing Pareto Distribution

7.323108e+02

1.034996e+01

3.241146e+01

Testing Constant Distribution

1.000000e+00

1.000000e+00

1.000000e+00

Testing Uniform Distribution

1.762401e+00

8.823415e+00

5.983056e+00

Testing exponential distribution

2.342635e+00

2.286844e+00

3.783593e+00

Testing hyperexponential Distribution

2.182741e-01

9.029152e-01

8.333935e-02

The results will be the same no matter how many times they are executed, because seed is fixed to 2.

If you change seed to 0, the first time you execute

Testing Pareto Distribution

2.384032e+00

5.089023e+00

4.105111e+01

Testing Constant Distribution

1.000000e+00

1.000000e+00

1.000000e+00

Testing Uniform Distribution

1.244962e+00

7.089372e+00

9.505300e+00

Testing exponential distribution

6.821317e-01

1.152453e+00

5.977962e+00

Testing hyperexponential Distribution

2.182741e-01

9.029152e-01

8.333935e-02

If seed is changed to 0, the second execution

Testing Pareto Distribution

2.095560e+00

3.138167e+00

4.567834e+00

Testing Constant Distribution

1.000000e+00

1.000000e+00

1.000000e+00

Testing Uniform Distribution

2.799521e+00

3.294468e-01

4.339498e+00

Testing exponential distribution

8.149149e-01

1.027426e+00

4.275210e+00

Testing hyperexponential Distribution

2.182741e-01

9.029152e-01

8.333935e-02

It can be seen that when seed is 0 o'clock,Pareto,Uniform, andexponential get different results.

Reference Address: http://wenku.baidu.com/view/e0320cd87f1922791688e82e.html###

"Go" ns2 random number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.