A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
main-random-variable-stream.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2008 Timo Bingmann
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Timo Bingmann <timo.bingmann@student.kit.edu>
7
*/
8
#include "ns3/command-line.h"
9
#include "ns3/double.h"
10
#include "ns3/gnuplot.h"
11
#include "ns3/integer.h"
12
#include "ns3/ptr.h"
13
#include "ns3/random-variable-stream.h"
14
#include "ns3/string.h"
15
16
#include <cmath>
17
#include <fstream>
18
#include <iostream>
19
#include <map>
20
21
/**
22
* \file
23
* \ingroup core-examples
24
* \ingroup randomvariable
25
* Example program illustrating use of RandomVariableStream
26
*/
27
28
using namespace
ns3
;
29
30
namespace
31
{
32
33
/**
34
* Round a double number to the given precision.
35
* For example, `dround(0.234, 0.1) = 0.2`
36
* and `dround(0.257, 0.1) = 0.3`
37
* \param [in] number The number to round.
38
* \param [in] precision The least significant digit to keep in the rounding.
39
* \returns \pname{number} rounded to \pname{precision}.
40
*/
41
double
42
dround(
double
number,
double
precision)
43
{
44
number /= precision;
45
if
(number >= 0)
46
{
47
number = std::floor(number + 0.5);
48
}
49
else
50
{
51
number = std::ceil(number - 0.5);
52
}
53
number *= precision;
54
return
number;
55
}
56
57
/**
58
* Generate a histogram from a RandomVariableStream.
59
* \param [in] rndvar The RandomVariableStream to sample.
60
* \param [in] probes The number of samples.
61
* \param [in] precision The precision to round samples to.
62
* \param [in] title The title for the histogram.
63
* \param [in] impulses Set the plot style to IMPULSES.
64
* \return The histogram as a GnuPlot data set.
65
*/
66
GnuplotDataset
67
Histogram
(
Ptr<RandomVariableStream>
rndvar,
68
unsigned
int
probes,
69
double
precision,
70
const
std::string& title,
71
bool
impulses =
false
)
72
{
73
typedef
std::map<double, unsigned int> histogram_maptype;
74
histogram_maptype histogram;
75
76
for
(
unsigned
int
i = 0; i < probes; ++i)
77
{
78
double
val = dround(rndvar->GetValue(), precision);
79
80
++histogram[val];
81
}
82
83
Gnuplot2dDataset
data
;
84
data
.SetTitle(title);
85
86
if
(impulses)
87
{
88
data
.SetStyle(
Gnuplot2dDataset::IMPULSES
);
89
}
90
91
for
(
auto
hi = histogram.begin(); hi != histogram.end(); ++hi)
92
{
93
data
.Add(hi->first, (
double
)hi->second / (
double
)probes / precision);
94
}
95
96
return
data
;
97
}
98
99
}
// unnamed namespace
100
101
int
102
main(
int
argc,
char
* argv[])
103
{
104
CommandLine
cmd(__FILE__);
105
cmd.Parse(argc, argv);
106
107
unsigned
int
probes = 1000000;
108
double
precision = 0.01;
109
110
const
std::string plotFile{
"main-random-variables"
};
111
GnuplotCollection
gnuplots(plotFile +
".pdf"
);
112
gnuplots.SetTerminal(
"pdf enhanced"
);
113
114
{
115
std::cout <<
"UniformRandomVariable......."
<< std::flush;
116
Gnuplot
plot;
117
plot.
SetTitle
(
"UniformRandomVariable"
);
118
plot.
AppendExtra
(
"set yrange [0:]"
);
119
120
auto
x
=
CreateObject<UniformRandomVariable>
();
121
x
->SetAttribute(
"Min"
,
DoubleValue
(0.0));
122
x
->SetAttribute(
"Max"
,
DoubleValue
(1.0));
123
124
plot.
AddDataset
(
Histogram
(x, probes, precision,
"UniformRandomVariable [0.0 .. 1.0)"
));
125
plot.
AddDataset
(
Gnuplot2dFunction
(
"1.0"
,
"0 <= x && x <= 1 ? 1.0 : 0"
));
126
127
gnuplots.AddPlot(plot);
128
std::cout <<
"done"
<< std::endl;
129
}
130
131
{
132
std::cout <<
"ExponentialRandomVariable..."
<< std::flush;
133
Gnuplot
plot;
134
plot.
SetTitle
(
"ExponentialRandomVariable"
);
135
plot.
AppendExtra
(
"set xrange [0:4]"
);
136
plot.
AppendExtra
(
"ExpDist(x,l) = 1/l * exp(-1/l * x)"
);
137
138
auto
x1 =
CreateObject<ExponentialRandomVariable>
();
139
x1->SetAttribute(
"Mean"
,
DoubleValue
(0.5));
140
141
plot.
AddDataset
(
Histogram
(x1, probes, precision,
"ExponentialRandomVariable m=0.5"
));
142
143
plot.
AddDataset
(
Gnuplot2dFunction
(
"ExponentialDistribution mean 0.5"
,
"ExpDist(x, 0.5)"
));
144
145
auto
x2 =
CreateObject<ExponentialRandomVariable>
();
146
x2->SetAttribute(
"Mean"
,
DoubleValue
(1.0));
147
148
plot.
AddDataset
(
Histogram
(x2, probes, precision,
"ExponentialRandomVariable m=1"
));
149
150
plot.
AddDataset
(
Gnuplot2dFunction
(
"ExponentialDistribution mean 1.0"
,
"ExpDist(x, 1.0)"
));
151
152
auto
x3 =
CreateObject<ExponentialRandomVariable>
();
153
x3->SetAttribute(
"Mean"
,
DoubleValue
(1.5));
154
155
plot.
AddDataset
(
Histogram
(x3, probes, precision,
"ExponentialRandomVariable m=1.5"
));
156
157
plot.
AddDataset
(
Gnuplot2dFunction
(
"ExponentialDistribution mean 1.5"
,
"ExpDist(x, 1.5)"
));
158
159
gnuplots.AddPlot(plot);
160
std::cout <<
"done"
<< std::endl;
161
}
162
163
{
164
std::cout <<
"ParetoRandomVariable........"
<< std::flush;
165
Gnuplot
plot;
166
plot.
SetTitle
(
"ParetoRandomVariable"
);
167
plot.
AppendExtra
(
"set xrange [0:4]"
);
168
169
auto
x1 =
CreateObject<ParetoRandomVariable>
();
170
x1->SetAttribute(
"Scale"
,
DoubleValue
(1.0));
171
x1->SetAttribute(
"Shape"
,
DoubleValue
(1.5));
172
173
plot.
AddDataset
(
174
Histogram
(x1, probes, precision,
"ParetoRandomVariable scale=1.0 shape=1.5"
));
175
176
auto
x2 =
CreateObject<ParetoRandomVariable>
();
177
x2->SetAttribute(
"Scale"
,
DoubleValue
(1.0));
178
x2->SetAttribute(
"Shape"
,
DoubleValue
(2.0));
179
180
plot.
AddDataset
(
181
Histogram
(x2, probes, precision,
"ParetoRandomVariable scale=1.0 shape=2.0"
));
182
183
auto
x3 =
CreateObject<ParetoRandomVariable>
();
184
x3->SetAttribute(
"Scale"
,
DoubleValue
(1.0));
185
x3->SetAttribute(
"Shape"
,
DoubleValue
(2.5));
186
187
plot.
AddDataset
(
188
Histogram
(x3, probes, precision,
"ParetoRandomVariable scale=1.0 shape=2.5"
));
189
190
gnuplots.AddPlot(plot);
191
std::cout <<
"done"
<< std::endl;
192
}
193
194
{
195
std::cout <<
"WeibullRandomVariable......."
<< std::flush;
196
Gnuplot
plot;
197
plot.
SetTitle
(
"WeibullRandomVariable"
);
198
plot.
AppendExtra
(
"set xrange [0:4]"
);
199
200
auto
x1 =
CreateObject<WeibullRandomVariable>
();
201
x1->SetAttribute(
"Scale"
,
DoubleValue
(1.0));
202
x1->SetAttribute(
"Shape"
,
DoubleValue
(1.0));
203
204
plot.
AddDataset
(
205
Histogram
(x1, probes, precision,
"WeibullRandomVariable scale=1.0 shape=1.0"
));
206
207
auto
x2 =
CreateObject<WeibullRandomVariable>
();
208
x2->SetAttribute(
"Scale"
,
DoubleValue
(1.0));
209
x2->SetAttribute(
"Shape"
,
DoubleValue
(2.0));
210
211
plot.
AddDataset
(
212
Histogram
(x2, probes, precision,
"WeibullRandomVariable scale=1.0 shape=2.0"
));
213
214
auto
x3 =
CreateObject<WeibullRandomVariable>
();
215
x3->SetAttribute(
"Scale"
,
DoubleValue
(1.0));
216
x3->SetAttribute(
"Shape"
,
DoubleValue
(3.0));
217
218
plot.
AddDataset
(
219
Histogram
(x3, probes, precision,
"WeibullRandomVariable scale=1.0 shape=3.0"
));
220
221
gnuplots.AddPlot(plot);
222
std::cout <<
"done"
<< std::endl;
223
}
224
225
{
226
std::cout <<
"NormalRandomVariable........"
<< std::flush;
227
Gnuplot
plot;
228
plot.
SetTitle
(
"NormalRandomVariable"
);
229
plot.
AppendExtra
(
"set xrange [-4:4]"
);
230
plot.
AppendExtra
(
231
"NormalDist(x,m,s) = 1 / (s * sqrt(2*pi)) * exp(-1.0 / 2.0 * ((x-m) / s)**2)"
);
232
233
auto
x1 =
CreateObject<NormalRandomVariable>
();
234
x1->SetAttribute(
"Mean"
,
DoubleValue
(0.0));
235
x1->SetAttribute(
"Variance"
,
DoubleValue
(1.0));
236
237
plot.
AddDataset
(
Histogram
(x1, probes, precision,
"NormalRandomVariable m=0.0 v=1.0"
));
238
239
plot.
AddDataset
(
Gnuplot2dFunction
(
"NormalDist {/Symbol m}=0.0 {/Symbol s}=1.0"
,
240
"NormalDist(x,0.0,1.0)"
));
241
242
auto
x2 =
CreateObject<NormalRandomVariable>
();
243
x2->SetAttribute(
"Mean"
,
DoubleValue
(0.0));
244
x2->SetAttribute(
"Variance"
,
DoubleValue
(2.0));
245
246
plot.
AddDataset
(
Histogram
(x2, probes, precision,
"NormalRandomVariable m=0.0 v=2.0"
));
247
248
plot.
AddDataset
(
Gnuplot2dFunction
(
"NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(2.0)"
,
249
"NormalDist(x,0.0,sqrt(2.0))"
));
250
251
auto
x3 =
CreateObject<NormalRandomVariable>
();
252
x3->SetAttribute(
"Mean"
,
DoubleValue
(0.0));
253
x3->SetAttribute(
"Variance"
,
DoubleValue
(3.0));
254
255
plot.
AddDataset
(
Histogram
(x3, probes, precision,
"NormalRandomVariable m=0.0 v=3.0"
));
256
257
plot.
AddDataset
(
Gnuplot2dFunction
(
"NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(3.0)"
,
258
"NormalDist(x,0.0,sqrt(3.0))"
));
259
260
gnuplots.AddPlot(plot);
261
std::cout <<
"done"
<< std::endl;
262
}
263
264
{
265
std::cout <<
"EmpiricalVariable..........."
<< std::flush;
266
Gnuplot
plot;
267
plot.
SetTitle
(
"EmpiricalRandomVariable"
);
268
plot.
AppendExtra
(
"set xrange [*:*]"
);
269
270
auto
x
=
CreateObject<EmpiricalRandomVariable>
();
271
x
->CDF(0.0, 0.0 / 15.0);
272
x
->CDF(0.2, 1.0 / 15.0);
273
x
->CDF(0.4, 3.0 / 15.0);
274
x
->CDF(0.6, 6.0 / 15.0);
275
x
->CDF(0.8, 10.0 / 15.0);
276
x
->CDF(1.0, 15.0 / 15.0);
277
278
plot.
AddDataset
(
279
Histogram
(x, probes, precision,
"EmpiricalRandomVariable (Sampling)"
,
true
));
280
281
x
->SetInterpolate(
true
);
282
plot.
AppendExtra
(
"set y2range [0:*]"
);
283
auto
d2 =
Histogram
(x, probes, precision,
"EmpiricalRandomVariable (Interpolate)"
);
284
d2.SetExtra(
" axis x1y2"
);
285
plot.
AddDataset
(d2);
286
287
gnuplots.AddPlot(plot);
288
std::cout <<
"done"
<< std::endl;
289
}
290
291
{
292
std::cout <<
"DeterministicVariable......."
<< std::flush;
293
Gnuplot
plot;
294
plot.
SetTitle
(
"DeterministicRandomVariable"
);
295
plot.
AppendExtra
(
"set xrange [*:*]"
);
296
297
auto
x1 =
CreateObject<DeterministicRandomVariable>
();
298
double
values[] = {0.0, 0.2, 0.2, 0.4, 0.2, 0.6, 0.8, 0.8, 1.0};
299
x1->SetValueArray(values,
sizeof
(values) /
sizeof
(values[0]));
300
301
plot.
AddDataset
(
Histogram
(x1, probes, precision,
"DeterministicRandomVariable"
,
true
));
302
303
gnuplots.AddPlot(plot);
304
std::cout <<
"done"
<< std::endl;
305
}
306
307
{
308
std::cout <<
"LogNormalRandomVariable....."
<< std::flush;
309
Gnuplot
plot;
310
plot.
SetTitle
(
"LogNormalRandomVariable"
);
311
plot.
AppendExtra
(
"set xrange [0:4]"
);
312
313
plot.
AppendExtra
(
"LogNormalDist(x,m,s) = 1.0/x * NormalDist(log(x), m, s)"
);
314
315
auto
x1 =
CreateObject<LogNormalRandomVariable>
();
316
x1->SetAttribute(
"Mu"
,
DoubleValue
(0.0));
317
x1->SetAttribute(
"Sigma"
,
DoubleValue
(1.0));
318
319
plot.
AddDataset
(
Histogram
(x1, probes, precision,
"LogNormalRandomVariable m=0.0 s=1.0"
));
320
321
plot.
AddDataset
(
322
Gnuplot2dFunction
(
"LogNormalDist(x, 0.0, 1.0)"
,
"LogNormalDist(x, 0.0, 1.0)"
));
323
324
auto
x2 =
CreateObject<LogNormalRandomVariable>
();
325
x2->SetAttribute(
"Mu"
,
DoubleValue
(0.0));
326
x2->SetAttribute(
"Sigma"
,
DoubleValue
(0.5));
327
328
plot.
AddDataset
(
Histogram
(x2, probes, precision,
"LogNormalRandomVariable m=0.0 s=0.5"
));
329
330
auto
x3 =
CreateObject<LogNormalRandomVariable>
();
331
x3->SetAttribute(
"Mu"
,
DoubleValue
(0.0));
332
x3->SetAttribute(
"Sigma"
,
DoubleValue
(0.25));
333
334
plot.
AddDataset
(
Histogram
(x3, probes, precision,
"LogNormalRandomVariable m=0.0 s=0.25"
));
335
336
plot.
AddDataset
(
337
Gnuplot2dFunction
(
"LogNormalDist(x, 0.0, 0.25)"
,
"LogNormalDist(x, 0.0, 0.25)"
));
338
339
auto
x4 =
CreateObject<LogNormalRandomVariable>
();
340
x4->SetAttribute(
"Mu"
,
DoubleValue
(0.0));
341
x4->SetAttribute(
"Sigma"
,
DoubleValue
(0.125));
342
343
plot.
AddDataset
(
Histogram
(x4, probes, precision,
"LogNormalRandomVariable m=0.0 s=0.125"
));
344
345
auto
x5 =
CreateObject<LogNormalRandomVariable>
();
346
x5->SetAttribute(
"Mu"
,
DoubleValue
(0.0));
347
x5->SetAttribute(
"Sigma"
,
DoubleValue
(2.0));
348
349
plot.
AddDataset
(
Histogram
(x5, probes, precision,
"LogNormalRandomVariable m=0.0 s=2.0"
));
350
351
plot.
AddDataset
(
352
Gnuplot2dFunction
(
"LogNormalDist(x, 0.0, 2.0)"
,
"LogNormalDist(x, 0.0, 2.0)"
));
353
354
auto
x6 =
CreateObject<LogNormalRandomVariable>
();
355
x6->SetAttribute(
"Mu"
,
DoubleValue
(0.0));
356
x6->SetAttribute(
"Sigma"
,
DoubleValue
(2.5));
357
358
plot.
AddDataset
(
Histogram
(x6, probes, precision,
"LogNormalRandomVariable m=0.0 s=2.5"
));
359
360
gnuplots.AddPlot(plot);
361
std::cout <<
"done"
<< std::endl;
362
}
363
364
{
365
std::cout <<
"TriangularRandomVariable...."
<< std::flush;
366
Gnuplot
plot;
367
plot.
SetTitle
(
"TriangularRandomVariable"
);
368
plot.
AppendExtra
(
"set xrange [*:*]"
);
369
370
auto
x1 =
CreateObject<TriangularRandomVariable>
();
371
x1->SetAttribute(
"Min"
,
DoubleValue
(0.0));
372
x1->SetAttribute(
"Max"
,
DoubleValue
(1.0));
373
x1->SetAttribute(
"Mean"
,
DoubleValue
(0.5));
374
375
plot.
AddDataset
(
376
Histogram
(x1, probes, precision,
"TriangularRandomVariable [0.0 .. 1.0) m=0.5"
));
377
378
auto
x2 =
CreateObject<TriangularRandomVariable>
();
379
x2->SetAttribute(
"Min"
,
DoubleValue
(0.0));
380
x2->SetAttribute(
"Max"
,
DoubleValue
(1.0));
381
x2->SetAttribute(
"Mean"
,
DoubleValue
(0.4));
382
383
plot.
AddDataset
(
384
Histogram
(x2, probes, precision,
"TriangularRandomVariable [0.0 .. 1.0) m=0.4"
));
385
386
auto
x3 =
CreateObject<TriangularRandomVariable>
();
387
x3->SetAttribute(
"Min"
,
DoubleValue
(0.0));
388
x3->SetAttribute(
"Max"
,
DoubleValue
(1.0));
389
x3->SetAttribute(
"Mean"
,
DoubleValue
(0.65));
390
391
plot.
AddDataset
(
392
Histogram
(x3, probes, precision,
"TriangularRandomVariable [0.0 .. 1.0) m=0.65"
));
393
394
gnuplots.AddPlot(plot);
395
std::cout <<
"done"
<< std::endl;
396
}
397
398
{
399
std::cout <<
"GammaRandomVariable........."
<< std::flush;
400
Gnuplot
plot;
401
plot.
SetTitle
(
"GammaRandomVariable"
);
402
plot.
AppendExtra
(
"set xrange [0:10]"
);
403
plot.
AppendExtra
(
"set yrange [0:1]"
);
404
plot.
AppendExtra
(
"GammaDist(x,a,b) = x**(a-1) * 1/b**a * exp(-x/b) / gamma(a)"
);
405
406
plot.
AppendExtra
(
407
"set label 1 '{/Symbol g}(x,{/Symbol a},{/Symbol b}) = x^{/Symbol a-1} e^{-x {/Symbol "
408
"b}^{-1}} ( {/Symbol b}^{/Symbol a} {/Symbol G}({/Symbol a}) )^{-1}' at 0.7, 0.9"
);
409
410
auto
x1 =
CreateObject<GammaRandomVariable>
();
411
x1->SetAttribute(
"Alpha"
,
DoubleValue
(1.0));
412
x1->SetAttribute(
"Beta"
,
DoubleValue
(1.0));
413
414
plot.
AddDataset
(
Histogram
(x1, probes, precision,
"GammaRandomVariable a=1.0 b=1.0"
));
415
416
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 1.0, 1.0)"
,
"GammaDist(x, 1.0, 1.0)"
));
417
418
auto
x2 =
CreateObject<GammaRandomVariable>
();
419
x2->SetAttribute(
"Alpha"
,
DoubleValue
(1.5));
420
x2->SetAttribute(
"Beta"
,
DoubleValue
(1.0));
421
422
plot.
AddDataset
(
Histogram
(x2, probes, precision,
"GammaRandomVariable a=1.5 b=1.0"
));
423
424
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 1.5, 1.0)"
,
"GammaDist(x, 1.5, 1.0)"
));
425
426
auto
x3 =
CreateObject<GammaRandomVariable>
();
427
x3->SetAttribute(
"Alpha"
,
DoubleValue
(2.0));
428
x3->SetAttribute(
"Beta"
,
DoubleValue
(1.0));
429
430
plot.
AddDataset
(
Histogram
(x3, probes, precision,
"GammaRandomVariable a=2.0 b=1.0"
));
431
432
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 2.0, 1.0)"
,
"GammaDist(x, 2.0, 1.0)"
));
433
434
auto
x4 =
CreateObject<GammaRandomVariable>
();
435
x4->SetAttribute(
"Alpha"
,
DoubleValue
(4.0));
436
x4->SetAttribute(
"Beta"
,
DoubleValue
(1.0));
437
438
plot.
AddDataset
(
Histogram
(x4, probes, precision,
"GammaRandomVariable a=4.0 b=1.0"
));
439
440
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 4.0, 1.0)"
,
"GammaDist(x, 4.0, 1.0)"
));
441
442
auto
x5 =
CreateObject<GammaRandomVariable>
();
443
x5->SetAttribute(
"Alpha"
,
DoubleValue
(2.0));
444
x5->SetAttribute(
"Beta"
,
DoubleValue
(2.0));
445
446
plot.
AddDataset
(
Histogram
(x5, probes, precision,
"GammaRandomVariable a=2.0 b=2.0"
));
447
448
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 2.0, 2.0)"
,
"GammaDist(x, 2.0, 2.0)"
));
449
450
auto
x6 =
CreateObject<GammaRandomVariable>
();
451
x6->SetAttribute(
"Alpha"
,
DoubleValue
(2.5));
452
x6->SetAttribute(
"Beta"
,
DoubleValue
(3.0));
453
454
plot.
AddDataset
(
Histogram
(x6, probes, precision,
"GammaRandomVariable a=2.5 b=3.0"
));
455
456
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 2.5, 3.0)"
,
"GammaDist(x, 2.5, 3.0)"
));
457
458
auto
x7 =
CreateObject<GammaRandomVariable>
();
459
x7->SetAttribute(
"Alpha"
,
DoubleValue
(2.5));
460
x7->SetAttribute(
"Beta"
,
DoubleValue
(4.5));
461
462
plot.
AddDataset
(
Histogram
(x7, probes, precision,
"GammaRandomVariable a=2.5 b=4.5"
));
463
464
plot.
AddDataset
(
Gnuplot2dFunction
(
"{/Symbol g}(x, 2.5, 4.5)"
,
"GammaDist(x, 2.5, 4.5)"
));
465
466
gnuplots.AddPlot(plot);
467
std::cout <<
"done"
<< std::endl;
468
}
469
470
{
471
std::cout <<
"ErlangRandomVariable........"
<< std::flush;
472
Gnuplot
plot;
473
plot.
SetTitle
(
"ErlangRandomVariable"
);
474
plot.
AppendExtra
(
"set xrange [0:10]"
);
475
plot.
AppendExtra
(
"ErlangDist(x,k,l) = x**(k-1) * 1/l**k * exp(-x/l) / (k-1)!"
);
476
477
plot.
AppendExtra
(
"set label 1 'Erlang(x,k,{/Symbol l}) = x^{k-1} e^{-x {/Symbol l}^{-1}} ( "
478
"{/Symbol l}^k (k-1)! )^{-1}' at 0.7, 0.9"
);
479
480
auto
x1 =
CreateObject<ErlangRandomVariable>
();
481
x1->SetAttribute(
"K"
,
IntegerValue
(1));
482
x1->SetAttribute(
"Lambda"
,
DoubleValue
(1.0));
483
484
plot.
AddDataset
(
485
Histogram
(x1, probes, precision,
"ErlangRandomVariable k=1 {/Symbol l}=1.0"
));
486
487
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 1, 1.0)"
,
"ErlangDist(x, 1, 1.0)"
));
488
489
auto
x2 =
CreateObject<ErlangRandomVariable>
();
490
x2->SetAttribute(
"K"
,
IntegerValue
(2));
491
x2->SetAttribute(
"Lambda"
,
DoubleValue
(1.0));
492
493
plot.
AddDataset
(
494
Histogram
(x2, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=1.0"
));
495
496
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 2, 1.0)"
,
"ErlangDist(x, 2, 1.0)"
));
497
498
auto
x3 =
CreateObject<ErlangRandomVariable>
();
499
x3->SetAttribute(
"K"
,
IntegerValue
(3));
500
x3->SetAttribute(
"Lambda"
,
DoubleValue
(1.0));
501
502
plot.
AddDataset
(
503
Histogram
(x3, probes, precision,
"ErlangRandomVariable k=3 {/Symbol l}=1.0"
));
504
505
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 3, 1.0)"
,
"ErlangDist(x, 3, 1.0)"
));
506
507
auto
x4 =
CreateObject<ErlangRandomVariable>
();
508
x4->SetAttribute(
"K"
,
IntegerValue
(5));
509
x4->SetAttribute(
"Lambda"
,
DoubleValue
(1.0));
510
511
plot.
AddDataset
(
512
Histogram
(x4, probes, precision,
"ErlangRandomVariable k=5 {/Symbol l}=1.0"
));
513
514
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 5, 1.0)"
,
"ErlangDist(x, 5, 1.0)"
));
515
516
auto
x5 =
CreateObject<ErlangRandomVariable>
();
517
x5->SetAttribute(
"K"
,
IntegerValue
(2));
518
x5->SetAttribute(
"Lambda"
,
DoubleValue
(2.0));
519
520
plot.
AddDataset
(
521
Histogram
(x5, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=2.0"
));
522
523
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 2, 2.0)"
,
"ErlangDist(x, 2, 2.0)"
));
524
525
auto
x6 =
CreateObject<ErlangRandomVariable>
();
526
x6->SetAttribute(
"K"
,
IntegerValue
(2));
527
x6->SetAttribute(
"Lambda"
,
DoubleValue
(3.0));
528
529
plot.
AddDataset
(
530
Histogram
(x6, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=3.0"
));
531
532
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 2, 3.0)"
,
"ErlangDist(x, 2, 3.0)"
));
533
534
auto
x7 =
CreateObject<ErlangRandomVariable>
();
535
x7->SetAttribute(
"K"
,
IntegerValue
(2));
536
x7->SetAttribute(
"Lambda"
,
DoubleValue
(5.0));
537
538
plot.
AddDataset
(
539
Histogram
(x7, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=5.0"
));
540
541
plot.
AddDataset
(
Gnuplot2dFunction
(
"Erlang(x, 2, 5.0)"
,
"ErlangDist(x, 2, 5.0)"
));
542
543
gnuplots.AddPlot(plot);
544
std::cout <<
"done"
<< std::endl;
545
}
546
547
{
548
std::cout <<
"BinomialRandomVariable......."
<< std::flush;
549
Gnuplot
plot;
550
plot.
SetTitle
(
"BinomialRandomVariable"
);
551
plot.
AppendExtra
(
"set yrange [0:10]"
);
552
553
auto
x
=
CreateObject<BinomialRandomVariable>
();
554
x
->SetAttribute(
"Trials"
,
IntegerValue
(10));
555
x
->SetAttribute(
"Probability"
,
DoubleValue
(0.5));
556
557
plot.
AddDataset
(
Histogram
(x, probes, precision,
"BinomialRandomVariable n=10 p=0.5"
));
558
559
gnuplots.AddPlot(plot);
560
std::cout <<
"done"
<< std::endl;
561
}
562
563
{
564
std::cout <<
"BernoulliRandomVariable......."
<< std::flush;
565
Gnuplot
plot;
566
plot.
SetTitle
(
"BernoulliRandomVariable"
);
567
plot.
AppendExtra
(
"set yrange [0:1]"
);
568
569
auto
x
=
CreateObject<BernoulliRandomVariable>
();
570
x
->SetAttribute(
"Probability"
,
DoubleValue
(0.5));
571
572
plot.
AddDataset
(
Histogram
(x, probes, precision,
"BernoulliRandomVariable p=0.5"
));
573
574
gnuplots.AddPlot(plot);
575
std::cout <<
"done"
<< std::endl;
576
}
577
578
{
579
std::string gnuFile = plotFile +
".plt"
;
580
std::cout <<
"Writing Gnuplot file: "
<< gnuFile <<
"..."
<< std::flush;
581
std::ofstream gnuStream(gnuFile);
582
gnuplots.GenerateOutput(gnuStream);
583
gnuStream.close();
584
585
std::cout <<
"done\nGenerating "
<< plotFile <<
".pdf..."
<< std::flush;
586
std::string shellcmd =
"gnuplot "
+ gnuFile;
587
int
returnValue = std::system(shellcmd.c_str());
588
if
(returnValue)
589
{
590
std::cout << std::endl
591
<<
"Error: shell command failed with "
<< returnValue <<
"; no pdf generated."
592
<< std::endl;
593
}
594
else
595
{
596
std::cout <<
"done"
<< std::endl;
597
}
598
}
599
600
return
0;
601
}
double
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition
double.h:31
ns3::Gnuplot2dDataset
Class to represent a 2D points plot.
Definition
gnuplot.h:105
ns3::Gnuplot2dDataset::IMPULSES
@ IMPULSES
Definition
gnuplot.h:116
ns3::Gnuplot2dFunction
Class to represent a 2D function expression plot.
Definition
gnuplot.h:233
ns3::GnuplotCollection
a simple class to group together multiple gnuplots into one file, e.g.
Definition
gnuplot.h:473
ns3::GnuplotDataset
Abstract class to store a plot line to be used by ns3::Gnuplot.
Definition
gnuplot.h:28
ns3::Gnuplot
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition
gnuplot.h:359
ns3::Gnuplot::AddDataset
void AddDataset(const GnuplotDataset &dataset)
Definition
gnuplot.cc:785
ns3::Gnuplot::AppendExtra
void AppendExtra(const std::string &extra)
Definition
gnuplot.cc:778
ns3::Gnuplot::SetTitle
void SetTitle(const std::string &title)
Definition
gnuplot.cc:759
ns3::Histogram
Class used to store data and make an histogram of the data frequency.
Definition
histogram.h:35
ns3::IntegerValue
Hold a signed integer type.
Definition
integer.h:34
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition
object.h:619
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
two-ray-to-three-gpp-ch-calibration.x
x
Definition
two-ray-to-three-gpp-ch-calibration.py:593
data
uint8_t data[writeSize]
Definition
socket-bound-tcp-static-routing.cc:41
src
core
examples
main-random-variable-stream.cc
Generated on Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0