View | Details | Raw Unified | Return to bug 475
Collapse All | Expand All

(-)cc06d903ca09 (+377 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2008 Timo Bingmann
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Timo Bingmann <timo.bingmann@student.kit.edu>
19
 */
20
#include "ns3/random-variable.h"
21
#include "ns3/gnuplot.h"
22
#include <map>
23
#include <cmath>
24
25
using namespace ns3;
26
27
/// Round a double number to the given precision. e.g. dround(0.234, 0.1) = 0.2
28
/// and dround(0.257, 0.1) = 0.3
29
double dround(double number, double precision)
30
{
31
  number /= precision;
32
  if (number >= 0)
33
    number = std::floor(number + 0.5);
34
  else
35
    number = std::ceil(number - 0.5);
36
  number *= precision;
37
  return number;
38
}
39
40
static GnuplotDataset
41
Histogramm (RandomVariable rndvar, unsigned int probes, double precision, const std::string& title, bool notcontinous = false)
42
{
43
  typedef std::map<double, unsigned int> histogramm_maptype;
44
  histogramm_maptype histogramm;
45
46
  for(unsigned int i = 0; i < probes; ++i)
47
    {
48
      double val = dround( rndvar.GetValue(), precision );
49
50
      ++histogramm[val];
51
    }
52
53
  Gnuplot2dDataset data;
54
  data.SetTitle(title);
55
56
  if (notcontinous)
57
    {
58
      data.SetStyle(Gnuplot2dDataset::IMPULSES);
59
    }
60
61
  for(histogramm_maptype::const_iterator hi = histogramm.begin();
62
      hi != histogramm.end(); ++hi)
63
    {
64
      data.Add(hi->first, (double)hi->second / (double)probes / precision);
65
    }
66
67
  return data;
68
}
69
70
int main (int argc, char *argv[])
71
{
72
  unsigned int probes = 1000000;
73
  double precision = 0.01;
74
75
  GnuplotCollection gnuplots("main-random-variables.pdf");
76
  gnuplots.SetTerminal("pdf enhanced");
77
78
  {
79
    Gnuplot plot;
80
    plot.SetTitle("UniformVariable");
81
    plot.AppendExtra("set yrange [0:]");
82
83
    plot.AddDataset( Histogramm(UniformVariable(0.0, 1.0), probes, precision,
84
                                "UniformVariable [0.0 .. 1.0)") );
85
86
    plot.AddDataset( Gnuplot2dFunction("0.1",
87
                                       "0 <= x && x <= 1 ? 1.0 : 0") );
88
89
    gnuplots.AddPlot(plot);
90
  }
91
92
  {
93
    Gnuplot plot;
94
    plot.SetTitle("ExponentialVariable");
95
    plot.AppendExtra("set xrange [0:8]");
96
    plot.AppendExtra("ExpDist(x,l) = 1/l * exp(-1/l * x)");
97
98
    plot.AddDataset( Histogramm(ExponentialVariable(0.5), probes, precision,
99
                                "ExponentialVariable m=0.5") );
100
101
    plot.AddDataset( Gnuplot2dFunction("ExponentialDistribution mean 0.5",
102
                                       "ExpDist(x, 0.5)") );
103
104
    plot.AddDataset( Histogramm(ExponentialVariable(1.0), probes, precision,
105
                                "ExponentialVariable m=1") );
106
107
    plot.AddDataset( Gnuplot2dFunction("ExponentialDistribution mean 1.0",
108
                                       "ExpDist(x, 1.0)") );
109
110
    plot.AddDataset( Histogramm(ExponentialVariable(1.5), probes, precision,
111
                                "ExponentialVariable m=1.5") );
112
113
    plot.AddDataset( Gnuplot2dFunction("ExponentialDistribution mean 1.5",
114
                                       "ExpDist(x, 1.5)") );
115
116
    gnuplots.AddPlot(plot);
117
  }
118
119
  {
120
    Gnuplot plot;
121
    plot.SetTitle("ParetoVariable");
122
    plot.AppendExtra("set xrange [0:2]");
123
124
    plot.AddDataset( Histogramm(ParetoVariable(1.0, 1.5), probes, precision,
125
                                "ParetoVariable m=1.0 s=1.5") );
126
127
    plot.AddDataset( Histogramm(ParetoVariable(1.0, 2.0), probes, precision,
128
                                "ParetoVariable m=1.0 s=2.0") );
129
130
    plot.AddDataset( Histogramm(ParetoVariable(1.0, 2.5), probes, precision,
131
                                "ParetoVariable m=1.0 s=2.5") );
132
133
    gnuplots.AddPlot(plot);
134
  }
135
136
  {
137
    Gnuplot plot;
138
    plot.SetTitle("WeibullVariable");
139
    plot.AppendExtra("set xrange [0:3]");
140
141
    plot.AddDataset( Histogramm(WeibullVariable(1.0, 1.0), probes, precision,
142
                                "WeibullVariable m=1.0 s=1.0") );
143
144
    plot.AddDataset( Histogramm(WeibullVariable(1.0, 2.0), probes, precision,
145
                                "WeibullVariable m=1.0 s=2.0") );
146
147
    plot.AddDataset( Histogramm(WeibullVariable(1.0, 3.0), probes, precision,
148
                                "WeibullVariable m=1.0 s=3.0") );
149
150
    gnuplots.AddPlot(plot);
151
  }
152
153
  {
154
    Gnuplot plot;
155
    plot.SetTitle("NormalVariable");
156
    plot.AppendExtra("set xrange [-3:3]");
157
    plot.AppendExtra("NormalDist(x,m,s) = 1 / (s * sqrt(2*pi)) * exp(-1.0 / 2.0 * ((x-m) / s)**2)");
158
159
    plot.AddDataset( Histogramm(NormalVariable(0.0, 1.0), probes, precision,
160
                                "NormalVariable m=0.0 v=1.0") );
161
162
    plot.AddDataset( Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=1.0",
163
                                       "NormalDist(x,0.0,1.0)") );
164
165
    plot.AddDataset( Histogramm(NormalVariable(0.0, 2.0), probes, precision,
166
                                "NormalVariable m=0.0 v=2.0") );
167
168
    plot.AddDataset( Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(2.0)",
169
                                       "NormalDist(x,0.0,sqrt(2.0))") );
170
171
    plot.AddDataset( Histogramm(NormalVariable(0.0, 3.0), probes, precision,
172
                                "NormalVariable m=0.0 v=3.0") );
173
174
    plot.AddDataset( Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(3.0)",
175
                                       "NormalDist(x,0.0,sqrt(3.0))") );
176
177
    gnuplots.AddPlot(plot);
178
  }
179
180
  {
181
    Gnuplot plot;
182
    plot.SetTitle("EmpiricalVariable");
183
    plot.AppendExtra("set xrange [*:*]");
184
185
    EmpiricalVariable emp1;
186
    emp1.CDF(0.0,  0.0 / 15.0);
187
    emp1.CDF(0.2,  1.0 / 15.0);
188
    emp1.CDF(0.4,  3.0 / 15.0);
189
    emp1.CDF(0.6,  6.0 / 15.0);
190
    emp1.CDF(0.8, 10.0 / 15.0);
191
    emp1.CDF(1.0, 15.0 / 15.0);
192
193
    plot.AddDataset( Histogramm(emp1, probes, precision,
194
                                "EmpiricalVariable (Stairs)") );
195
196
    gnuplots.AddPlot(plot);
197
  }
198
199
  {
200
    Gnuplot plot;
201
    plot.SetTitle("DeterministicVariable");
202
    plot.AppendExtra("set xrange [*:*]");
203
204
    double values[] = { 0.0, 0.2, 0.2, 0.4, 0.2, 0.6, 0.8, 0.8, 1.0 };
205
    DeterministicVariable det1 (values, sizeof(values) / sizeof(values[0]));
206
207
    plot.AddDataset( Histogramm(det1, probes, precision,
208
                                "DeterministicVariable", true) );
209
210
    gnuplots.AddPlot(plot);
211
  }
212
213
  {
214
    Gnuplot plot;
215
    plot.SetTitle("LogNormalVariable");
216
    plot.AppendExtra("set xrange [0:3]");
217
218
    plot.AppendExtra("LogNormalDist(x,m,s) = 1.0/x * NormalDist(log(x), m, s)");
219
220
    plot.AddDataset( Histogramm(LogNormalVariable(0.0, 1.0), probes, precision,
221
                                "LogNormalVariable m=0.0 s=1.0") );
222
223
    plot.AddDataset( Gnuplot2dFunction("LogNormalDist(x, 0.0, 1.0)",
224
                                       "LogNormalDist(x, 0.0, 1.0)") );
225
226
    plot.AddDataset( Histogramm(LogNormalVariable(0.0, 0.5), probes, precision,
227
                                "LogNormalVariable m=0.0 s=0.5") );
228
229
    plot.AddDataset( Histogramm(LogNormalVariable(0.0, 0.25), probes, precision,
230
                                "LogNormalVariable m=0.0 s=0.25") );
231
232
    plot.AddDataset( Gnuplot2dFunction("LogNormalDist(x, 0.0, 0.25)",
233
                                       "LogNormalDist(x, 0.0, 0.25)") );
234
235
    plot.AddDataset( Histogramm(LogNormalVariable(0.0, 0.125), probes, precision,
236
                                "LogNormalVariable m=0.0 s=0.125") );
237
238
    plot.AddDataset( Histogramm(LogNormalVariable(0.0, 2.0), probes, precision,
239
                                "LogNormalVariable m=0.0 s=2.0") );
240
241
    plot.AddDataset( Gnuplot2dFunction("LogNormalDist(x, 0.0, 2.0)",
242
                                       "LogNormalDist(x, 0.0, 2.0)") );
243
244
    plot.AddDataset( Histogramm(LogNormalVariable(0.0, 2.5), probes, precision,
245
                                "LogNormalVariable m=0.0 s=2.5") );
246
247
    gnuplots.AddPlot(plot);
248
  }
249
250
  {
251
    Gnuplot plot;
252
    plot.SetTitle("TriangularVariable");
253
    plot.AppendExtra("set xrange [*:*]");
254
255
    plot.AddDataset( Histogramm(TriangularVariable(0.0, 1.0, 0.5), probes, precision,
256
                                "TriangularVariable [0.0 .. 1.0) m=0.5") );
257
258
    plot.AddDataset( Histogramm(TriangularVariable(0.0, 1.0, 0.4), probes, precision,
259
                                "TriangularVariable [0.0 .. 1.0) m=0.4") );
260
261
    plot.AddDataset( Histogramm(TriangularVariable(0.0, 1.0, 0.65), probes, precision,
262
                                "TriangularVariable [0.0 .. 1.0) m=0.65") );
263
264
    gnuplots.AddPlot(plot);
265
  }
266
267
  {
268
    Gnuplot plot;
269
    plot.SetTitle("GammaVariable");
270
    plot.AppendExtra("set xrange [0:10]");
271
    plot.AppendExtra("set yrange [0:1]");
272
    plot.AppendExtra("GammaDist(x,a,b) = x**(a-1) * 1/b**a * exp(-x/b) / gamma(a)");
273
274
    plot.AppendExtra("set label 1 '{/Symbol g}(x,{/Symbol a},{/Symbol b}) = x^{/Symbol a-1} e^{-x {/Symbol b}^{-1}} ( {/Symbol b}^{/Symbol a} {/Symbol G}({/Symbol a}) )^{-1}' at 0.7, 0.9");
275
276
    plot.AddDataset( Histogramm(GammaVariable(1.0, 1.0), probes, precision,
277
                                "GammaVariable a=1.0 b=1.0") );
278
279
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 1.0, 1.0)",
280
                                       "GammaDist(x, 1.0, 1.0)") );
281
282
    plot.AddDataset( Histogramm(GammaVariable(1.5, 1.0), probes, precision,
283
                                "GammaVariable a=1.5 b=1.0") );
284
285
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 1.5, 1.0)",
286
                                       "GammaDist(x, 1.5, 1.0)") );
287
288
    plot.AddDataset( Histogramm(GammaVariable(2.0, 1.0), probes, precision,
289
                                "GammaVariable a=2.0 b=1.0") );
290
291
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.0, 1.0)",
292
                                       "GammaDist(x, 2.0, 1.0)") );
293
294
    plot.AddDataset( Histogramm(GammaVariable(4.0, 1.0), probes, precision,
295
                                "GammaVariable a=4.0 b=1.0") );
296
297
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 4.0, 1.0)",
298
                                       "GammaDist(x, 4.0, 1.0)") );
299
300
    plot.AddDataset( Histogramm(GammaVariable(2.0, 2.0), probes, precision,
301
                                "GammaVariable a=2.0 b=2.0") );
302
303
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.0, 2.0)",
304
                                       "GammaDist(x, 2.0, 2.0)") );
305
306
    plot.AddDataset( Histogramm(GammaVariable(2.5, 3.0), probes, precision,
307
                                "GammaVariable a=2.5 b=3.0") );
308
309
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.5, 3.0)",
310
                                       "GammaDist(x, 2.5, 3.0)") );
311
312
    plot.AddDataset( Histogramm(GammaVariable(2.5, 4.5), probes, precision,
313
                                "GammaVariable a=2.5 b=4.5") );
314
315
    plot.AddDataset( Gnuplot2dFunction("{/Symbol g}(x, 2.5, 4.5)",
316
                                       "GammaDist(x, 2.5, 4.5)") );
317
318
    gnuplots.AddPlot(plot);
319
  }
320
321
  {
322
    Gnuplot plot;
323
    plot.SetTitle("ErlangVariable");
324
    plot.AppendExtra("set xrange [0:10]");
325
    plot.AppendExtra("ErlangDist(x,k,l) = x**(k-1) * 1/l**k * exp(-x/l) / (k-1)!");
326
327
    plot.AppendExtra("set label 1 'Erlang(x,k,{/Symbol l}) = x^{k-1} e^{-x {/Symbol l}^{-1}} ( {/Symbol l}^k (k-1)! )^{-1}' at 0.7, 0.9");
328
329
    plot.AddDataset( Histogramm(ErlangVariable(1, 1.0), probes, precision,
330
                                "ErlangVariable k=1 {/Symbol l}=1.0") );
331
332
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 1, 1.0)",
333
                                       "ErlangDist(x, 1, 1.0)") );
334
335
    plot.AddDataset( Histogramm(ErlangVariable(2, 1.0), probes, precision,
336
                                "ErlangVariable k=2 {/Symbol l}=1.0") );
337
338
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 1.0)",
339
                                       "ErlangDist(x, 2, 1.0)") );
340
341
    plot.AddDataset( Histogramm(ErlangVariable(3, 1.0), probes, precision,
342
                                "ErlangVariable k=3 {/Symbol l}=1.0") );
343
344
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 3, 1.0)",
345
                                       "ErlangDist(x, 3, 1.0)") );
346
347
    plot.AddDataset( Histogramm(ErlangVariable(5, 1.0), probes, precision,
348
                                "ErlangVariable k=5 {/Symbol l}=1.0") );
349
350
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 5, 1.0)",
351
                                       "ErlangDist(x, 5, 1.0)") );
352
353
    plot.AddDataset( Histogramm(ErlangVariable(2, 2.0), probes, precision,
354
                                "ErlangVariable k=2 {/Symbol l}=2.0") );
355
356
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 2.0)",
357
                                       "ErlangDist(x, 2, 2.0)") );
358
359
    plot.AddDataset( Histogramm(ErlangVariable(2, 3.0), probes, precision,
360
                                "ErlangVariable k=2 {/Symbol l}=3.0") );
361
362
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 3.0)",
363
                                       "ErlangDist(x, 2, 3.0)") );
364
365
    plot.AddDataset( Histogramm(ErlangVariable(2, 5.0), probes, precision,
366
                                "ErlangVariable k=2 {/Symbol l}=5.0") );
367
368
    plot.AddDataset( Gnuplot2dFunction("Erlang(x, 2, 5.0)",
369
                                       "ErlangDist(x, 2, 5.0)") );
370
371
    gnuplots.AddPlot(plot);
372
  }
373
374
  gnuplots.GenerateOutput(std::cout);
375
376
  return 0;
377
}
(-)a/samples/wscript (+3 lines)
 Lines 10-15   def build(bld): Link Here 
10
    obj = bld.create_ns3_program('main-simulator')
10
    obj = bld.create_ns3_program('main-simulator')
11
    obj.source = 'main-simulator.cc'
11
    obj.source = 'main-simulator.cc'
12
12
13
    obj = bld.create_ns3_program('main-random-variable')
14
    obj.source = 'main-random-variable.cc'
15
13
    obj = bld.create_ns3_program('main-packet-header', ['common', 'simulator'])
16
    obj = bld.create_ns3_program('main-packet-header', ['common', 'simulator'])
14
    obj.source = 'main-packet-header.cc'
17
    obj.source = 'main-packet-header.cc'
15
18
(-)a/src/core/random-variable.cc (+220 lines)
 Lines 1220-1225   LogNormalVariable::LogNormalVariable (do Link Here 
1220
1220
1221
//-----------------------------------------------------------------------------
1221
//-----------------------------------------------------------------------------
1222
//-----------------------------------------------------------------------------
1222
//-----------------------------------------------------------------------------
1223
// GammaVariableImpl
1224
class GammaVariableImpl : public RandomVariableBase
1225
{
1226
public:
1227
  /**
1228
   * \param alpha alpha parameter of the gamma distribution
1229
   * \param beta beta parameter of the gamma distribution
1230
   */
1231
  GammaVariableImpl (double alpha, double beta);
1232
1233
  /**
1234
   * \return A random value from this distribution
1235
   */
1236
  virtual double GetValue ();
1237
1238
  /**
1239
   * \return A random value from the gamma distribution with parameters alpha
1240
   * and beta
1241
   */
1242
  double GetValue(double alpha, double beta);
1243
1244
  virtual RandomVariableBase* Copy(void) const;
1245
1246
private:
1247
  double m_alpha;
1248
  double m_beta;
1249
  NormalVariable m_normal;
1250
};
1251
1252
1253
RandomVariableBase* GammaVariableImpl::Copy () const
1254
{
1255
  return new GammaVariableImpl (m_alpha, m_beta);
1256
}
1257
1258
GammaVariableImpl::GammaVariableImpl (double alpha, double beta)
1259
  : m_alpha(alpha), m_beta(beta) 
1260
{
1261
}
1262
1263
double
1264
GammaVariableImpl::GetValue ()
1265
{
1266
  return GetValue(m_alpha, m_beta);
1267
}
1268
1269
/*
1270
  The code for the following generator functions was adapted from ns-2
1271
  tools/ranvar.cc
1272
1273
  Originally the algorithm was devised by Marsaglia in 2000:
1274
  G. Marsaglia, W. W. Tsang: A simple method for gereating Gamma variables
1275
  ACM Transactions on mathematical software, Vol. 26, No. 3, Sept. 2000
1276
1277
  The Gamma distribution density function has the form 
1278
1279
	                     x^(alpha-1) * exp(-x/beta)
1280
	p(x; alpha, beta) = ----------------------------
1281
                             beta^alpha * Gamma(alpha)
1282
1283
  for x > 0.
1284
*/
1285
double
1286
GammaVariableImpl::GetValue (double alpha, double beta)
1287
{
1288
  if(!m_generator)
1289
    {
1290
      m_generator = new RngStream();
1291
    }
1292
1293
  if (alpha < 1)
1294
    {
1295
      double u = m_generator->RandU01 ();
1296
      return GetValue(1.0 + alpha, beta) * pow (u, 1.0 / alpha);
1297
    }
1298
	
1299
  double x, v, u;
1300
  double d = alpha - 1.0 / 3.0;
1301
  double c = (1.0 / 3.0) / sqrt (d);
1302
1303
  while (1)
1304
    {
1305
      do
1306
        {
1307
          x = m_normal.GetValue ();
1308
          v = 1.0 + c * x;
1309
        } while (v <= 0);
1310
1311
      v = v * v * v;
1312
      u = m_generator->RandU01 ();
1313
      if (u < 1 - 0.0331 * x * x * x * x)
1314
        break;
1315
      if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
1316
        break;
1317
    }
1318
1319
  return beta * d * v;
1320
}
1321
1322
GammaVariable::GammaVariable ()
1323
  : RandomVariable (GammaVariableImpl (1.0, 1.0))
1324
{
1325
}
1326
1327
GammaVariable::GammaVariable (double alpha, double beta)
1328
  : RandomVariable (GammaVariableImpl (alpha, beta))
1329
{
1330
}
1331
1332
double GammaVariable::GetValue(void) const
1333
{
1334
  return this->RandomVariable::GetValue ();
1335
}
1336
1337
double GammaVariable::GetValue(double alpha, double beta) const
1338
{
1339
  return ((GammaVariableImpl*)Peek())->GetValue(alpha, beta);
1340
}
1341
1342
//-----------------------------------------------------------------------------
1343
//-----------------------------------------------------------------------------
1344
// ErlangVariableImpl
1345
1346
class ErlangVariableImpl : public RandomVariableBase
1347
{
1348
public:
1349
  /**
1350
   * \param k k parameter of the Erlang distribution
1351
   * \param lambda lambda parameter of the Erlang distribution
1352
   */
1353
  ErlangVariableImpl (unsigned int k, double lambda);
1354
1355
  /**
1356
   * \return A random value from this distribution
1357
   */
1358
  virtual double GetValue ();
1359
1360
  /**
1361
   * \return A random value from the Erlang distribution with parameters k and
1362
   * lambda.
1363
   */
1364
  double GetValue(unsigned int k, double lambda);
1365
1366
  virtual RandomVariableBase* Copy(void) const;
1367
1368
private:
1369
  unsigned int m_k;
1370
  double m_lambda;
1371
};
1372
1373
1374
RandomVariableBase* ErlangVariableImpl::Copy () const
1375
{
1376
  return new ErlangVariableImpl (m_k, m_lambda);
1377
}
1378
1379
ErlangVariableImpl::ErlangVariableImpl (unsigned int k, double lambda)
1380
  : m_k(k), m_lambda(lambda)
1381
{
1382
}
1383
1384
double
1385
ErlangVariableImpl::GetValue ()
1386
{
1387
  return GetValue(m_k, m_lambda);
1388
}
1389
1390
/*
1391
  The code for the following generator functions was adapted from ns-2
1392
  tools/ranvar.cc
1393
1394
  The Erlang distribution density function has the form 
1395
1396
	                   x^(k-1) * exp(-x/lambda)
1397
	p(x; k, lambda) = ---------------------------
1398
                             lambda^k * (k-1)!
1399
1400
  for x > 0.
1401
*/
1402
double
1403
ErlangVariableImpl::GetValue (unsigned int k, double lambda)
1404
{
1405
  if(!m_generator)
1406
    {
1407
      m_generator = new RngStream();
1408
    }
1409
1410
  ExponentialVariable exponential(lambda);
1411
1412
  double result = 0;
1413
  for (unsigned int i = 0; i < k; ++i)
1414
    {
1415
      result += exponential.GetValue();
1416
    }
1417
1418
  return result;
1419
}
1420
1421
ErlangVariable::ErlangVariable ()
1422
  : RandomVariable (ErlangVariableImpl (1, 1.0))
1423
{
1424
}
1425
1426
ErlangVariable::ErlangVariable (unsigned int k, double lambda)
1427
  : RandomVariable (ErlangVariableImpl (k, lambda))
1428
{
1429
}
1430
1431
double ErlangVariable::GetValue(void) const
1432
{
1433
  return this->RandomVariable::GetValue ();
1434
}
1435
1436
double ErlangVariable::GetValue(unsigned int k, double lambda) const
1437
{
1438
  return ((ErlangVariableImpl*)Peek())->GetValue(k, lambda);
1439
}
1440
1441
//-----------------------------------------------------------------------------
1442
//-----------------------------------------------------------------------------
1223
// TriangularVariableImpl methods
1443
// TriangularVariableImpl methods
1224
class TriangularVariableImpl : public RandomVariableBase {
1444
class TriangularVariableImpl : public RandomVariableBase {
1225
public:
1445
public:
(-)a/src/core/random-variable.h (-3 / +104 lines)
 Lines 36-42    Link Here 
36
 *
36
 *
37
 */
37
 */
38
38
39
namespace ns3{
39
namespace ns3 {
40
40
41
class RandomVariableBase;
41
class RandomVariableBase;
42
42
 Lines 422-428   public: Link Here 
422
   * value and a shape (alpha) parameter of 1.5.
422
   * value and a shape (alpha) parameter of 1.5.
423
   * \param m mean value of the distribution
423
   * \param m mean value of the distribution
424
   */
424
   */
425
   WeibullVariable(double m) ;
425
  WeibullVariable(double m) ;
426
426
427
  /**
427
  /**
428
   * Constructs a weibull random variable with the specified mean
428
   * Constructs a weibull random variable with the specified mean
 Lines 560-566   public: Link Here 
560
  explicit DeterministicVariable(double* d, uint32_t c);
560
  explicit DeterministicVariable(double* d, uint32_t c);
561
};
561
};
562
562
563
564
/**
563
/**
565
 * \brief Log-normal Distributed random var
564
 * \brief Log-normal Distributed random var
566
 * \ingroup randomvariable
565
 * \ingroup randomvariable
 Lines 594-599   public: Link Here 
594
};
593
};
595
594
596
/**
595
/**
596
 * \brief Gamma Distributed Random Variable
597
 * \ingroup randomvariable
598
 *
599
 * GammaVariable defines a random variable with gamma distribution. 
600
 *
601
 * This class supports the creation of objects that return random numbers
602
 * from a fixed gamma distribution. It also supports the generation of 
603
 * single random numbers from various gamma distributions.
604
 *
605
 * The probability density function is defined over the interval [0,+inf) as:
606
 * \f$ x^{\alpha-1} \frac{e^{-\frac{x}{\beta}}}{\beta^\alpha \Gamma(\alpha)}\f$
607
 * where \f$ mean = \alpha\beta \f$ and 
608
 * \f$ variance = \alpha \beta^2\f$
609
 */
610
class GammaVariable : public RandomVariable 
611
{
612
public:
613
  /**
614
   * Constructs a gamma random variable with alpha = 1.0 and beta = 1.0
615
   */
616
  GammaVariable ();
617
618
  /**
619
   * \param alpha alpha parameter of the gamma distribution
620
   * \param beta beta parameter of the gamma distribution
621
   */
622
  GammaVariable (double alpha, double beta);
623
624
  /**
625
   * \brief call RandomVariable::GetValue
626
   * \return A floating point random value 
627
   *
628
   * Note: we have to re-implement this method here because the method is
629
   * overloaded below for the two-argument variant and the c++ name resolution
630
   * rules don't work well with overloads split between parent and child
631
   * classes.
632
   */
633
  double GetValue (void) const;
634
  
635
  /**
636
   * \brief Returns a gamma random distributed double with parameters alpha and beta.
637
   * \param alpha alpha parameter of the gamma distribution
638
   * \param beta beta parameter of the gamma distribution
639
   * \return A floating point random value
640
   */
641
  double GetValue(double alpha, double beta) const;
642
};
643
644
/**
645
 * \brief Erlang Distributed Random Variable
646
 * \ingroup randomvariable
647
 *
648
 * ErlangVariable defines a random variable with Erlang distribution.
649
 *
650
 * The Erlang distribution is a special case of the Gamma distribution where k
651
 * (= alpha) is a non-negative integer. Erlang distributed variables can be
652
 * generated using a much faster algorithm than gamma variables.
653
 *
654
 * This class supports the creation of objects that return random numbers from
655
 * a fixed Erlang distribution. It also supports the generation of single
656
 * random numbers from various Erlang distributions.
657
 *
658
 * The probability density function is defined over the interval [0,+inf) as:
659
 * \f$ \frac{x^{k-1} e^{-\frac{x}{\lambda}}}{\lambda^k (k-1)!}\f$
660
 * where \f$ mean = k \lambda \f$ and 
661
 * \f$ variance = k \lambda^2\f$
662
 */
663
class ErlangVariable : public RandomVariable 
664
{
665
public:
666
  /**
667
   * Constructs an Erlang random variable with k = 1 and lambda = 1.0
668
   */
669
  ErlangVariable ();
670
671
  /**
672
   * \param k k parameter of the Erlang distribution. Must be a non-negative integer.
673
   * \param lambda lambda parameter of the Erlang distribution
674
   */
675
  ErlangVariable (unsigned int k, double lambda);
676
677
  /**
678
   * \brief call RandomVariable::GetValue
679
   * \return A floating point random value 
680
   *
681
   * Note: we have to re-implement this method here because the method is
682
   * overloaded below for the two-argument variant and the c++ name resolution
683
   * rules don't work well with overloads split between parent and child
684
   * classes.
685
   */
686
  double GetValue (void) const;
687
  
688
  /**
689
   * \brief Returns an Erlang random distributed double with parameters k and lambda.
690
   * \param k k parameter of the Erlang distribution. Must be a non-negative integer.
691
   * \param lambda lambda parameter of the Erlang distribution
692
   * \return A floating point random value
693
   */
694
  double GetValue(unsigned int k, double lambda) const;
695
};
696
697
/**
597
 * \brief Triangularly Distributed random var
698
 * \brief Triangularly Distributed random var
598
 * \ingroup randomvariable
699
 * \ingroup randomvariable
599
 * 
700
 * 

Return to bug 475