如何在ns2里产生随机数字

使用方法
set r1 [new RandomVariable/Pareto]$r1 set avg_ 10.0$r1 set shape_ 1.2 puts stdout "[$r1 value ]"
通过value就可以得到随机数。
然后是要修改随机seed种子,就要在tcl开始预定义部分加入
set val(seed) 0.0

下面是一些随机分布的设定:
1、均匀分布
Random Variable /Uniform
2、指数分布
Random Variable /Exponential
3、Pareto分布
Random Variable /Pareto
4、常分布
Random Variable /Constant
5、超招数分布
Random Variable /HyperExponential
6、经验分布
Random Variable /Empirical
7、正态分布
Random Variable /Normal
8、对数正态分布
Random Variable /LogNormal
Tags: RandomVariables, Settings
Related posts
  • No related posts. written by dobby

set ns [new Simulator]
set nf [open ex3out.nam w]
$ns namtrace-all $nf    
set tf [open ex3out.tr w]
$ns trace-all $tf
set windowVsTime [open win w]
set param [open parameters w]

#Define a 'finish' procedure
proc finish {} {
         global ns nf tf
         $ns flush-trace
         close $nf                  
         close $tf
         exec nam ex3out.nam &              
         exit 0
}
#Create bottleneck and dest nodes
set n2 [$ns node]
set n3 [$ns node]
#Create links between these nodes
$ns duplex-link $n2 $n3 0.7Mb 20ms DropTail
#Set the TCP client is 5
set NumbSrc 5
#设置总共的仿真时间
set Duration 10
#Source nodes
for {set j 1} {$j<=$NumbSrc} { incr j } {
set S($j) [$ns node]
}                
# Create a random generator for starting the ftp and for bottleneck link delays
# if the seed is 0, each time will get different num.
set rng [new RNG]
$rng seed 0
# paratmers for random variables for delays
set RVdly [new RandomVariable/Uniform]
$RVdly set min_ 1
$RVdly set max_ 5
$RVdly use-rng $rng
# parameters for random variables for begenning of ftp connections
set RVstart [new RandomVariable/Uniform]
$RVstart set min_ 0
$RVstart set max_ 7
$RVstart use-rng $rng  
#We define two random parameters for each connections
for {set i 1} {$i<=$NumbSrc} { incr i } {
set startT($i)   [expr [$RVstart value]]
set dly($i) [expr [$RVdly value]]
#把信息输入到文件中的基本方法
puts $param "dly($i) $dly($i) ms"
puts $param "startT($i)   $startT($i) sec"
}
#Links between source and bottleneck
for {set j 1} {$j<=$NumbSrc} { incr j } {
$ns duplex-link $S($j) $n2 10Mb $dly($j)ms DropTail
$ns queue-limit $S($j) $n2 100
}                        
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5      
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#TCP Sources
for {set j 1} {$j<=$NumbSrc} { incr j } {
set tcp_src($j) [new Agent/TCP/Reno]
}
#TCP Destinations
for {set j 1} {$j<=$NumbSrc} { incr j } {
set tcp_snk($j) [new Agent/TCPSink]
}  
#Connections
for {set j 1} {$j<=$NumbSrc} { incr j } {
$ns attach-agent $S($j) $tcp_src($j)
$ns attach-agent $n3 $tcp_snk($j)
$ns connect $tcp_src($j) $tcp_snk($j)
}  
#FTP sources
for {set j 1} {$j<=$NumbSrc} { incr j } {
set ftp($j) [$tcp_src($j) attach-source FTP]
}      
#Parametrisation of TCP sources
for {set j 1} {$j<=$NumbSrc} { incr j } {
$tcp_src($j) set packetSize_ 552
}                  
#Schedule events for the FTP agents:
for {set i 1} {$i<=$NumbSrc} { incr i } {
$ns at $startT($i) "$ftp($i) start"
$ns at $Duration "$ftp($i) stop"
}                    
proc plotWindow {tcpSource file k} {
global ns
set time 0.03
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$k $now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file $k" }
# The procedure will now be called for all tcp sources
#注意,5个TCP源的窗口记录都是从0.1开始的
for {set j 1} {$j<=$NumbSrc} { incr j } {
$ns at 0.1 "plotWindow $tcp_src($j) $windowVsTime $j"
}
$ns at [expr $Duration] "finish"
$ns run
以下是自己写的均值为10的服从指数分布的随机变量产生
set rng [new RNG]
$rng seed 0
set RVstart [new RandomVariable/Uniform]
$RVstart set min_ 0
$RVstart set max_ 1
$RVstart use-rng $rng  
for {set i 1} {$i<=20} { incr i } {
set a [expr [$RVstart value]]
set startT($i)   [expr log($a)*(-10)]
puts "$startT($i)"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值