A Discrete-Event Network Simulator
API
rng-stream.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (C) 2001 Pierre L'Ecuyer (lecuyer@iro.umontreal.ca)
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 // Modified for ns-3 by:
19 // - Rajib Bhattacharjea<raj.b@gatech.edu>
20 // - Mathieu Lacage <mathieu.lacage@gmail.com>
21 //
22 
23 #include <cstdlib>
24 #include <iostream>
25 #include "rng-stream.h"
26 #include "fatal-error.h"
27 #include "log.h"
28 
32 
33 namespace ns3 {
34 
35 // Note: Logging in this file is largely avoided due to the
36 // number of calls that are made to these functions and the possibility
37 // of causing recursions leading to stack overflow
38 NS_LOG_COMPONENT_DEFINE ("RngStream");
39 
40 } // namespace ns3
41 
42 
45 namespace
46 {
47 
50 typedef double Matrix[3][3];
51 
54 const double m1 = 4294967087.0;
55 
58 const double m2 = 4294944443.0;
59 
62 const double norm = 1.0 / (m1 + 1.0);
63 
66 const double a12 = 1403580.0;
67 
70 const double a13n = 810728.0;
71 
74 const double a21 = 527612.0;
75 
78 const double a23n = 1370589.0;
79 
82 const double two17 = 131072.0;
83 
86 const double two53 = 9007199254740992.0;
87 
90 const Matrix A1p0 = {
91  { 0.0, 1.0, 0.0 },
92  { 0.0, 0.0, 1.0 },
93  { -810728.0, 1403580.0, 0.0 }
94 };
95 
98 const Matrix A2p0 = {
99  { 0.0, 1.0, 0.0 },
100  { 0.0, 0.0, 1.0 },
101  { -1370589.0, 0.0, 527612.0 }
102 };
103 
104 
105 //-------------------------------------------------------------------------
117 //
118 double MultModM (double a, double s, double c, double m)
119 {
120  double v;
121  int32_t a1;
122 
123  v = a * s + c;
124 
125  if (v >= two53 || v <= -two53)
126  {
127  a1 = static_cast<int32_t> (a / two17);
128  a -= a1 * two17;
129  v = a1 * s;
130  a1 = static_cast<int32_t> (v / m);
131  v -= a1 * m;
132  v = v * two17 + a * s + c;
133  }
134 
135  a1 = static_cast<int32_t> (v / m);
136  /* in case v < 0)*/
137  if ((v -= a1 * m) < 0.0)
138  {
139  return v += m;
140  }
141  else
142  {
143  return v;
144  }
145 }
146 
147 
148 //-------------------------------------------------------------------------
157 //
158 void MatVecModM (const Matrix A, const double s[3], double v[3],
159  double m)
160 {
161  int i;
162  double x[3]; // Necessary if v = s
163 
164  for (i = 0; i < 3; ++i)
165  {
166  x[i] = MultModM (A[i][0], s[0], 0.0, m);
167  x[i] = MultModM (A[i][1], s[1], x[i], m);
168  x[i] = MultModM (A[i][2], s[2], x[i], m);
169  }
170  for (i = 0; i < 3; ++i)
171  {
172  v[i] = x[i];
173  }
174 }
175 
176 
177 //-------------------------------------------------------------------------
186 //
187 void MatMatModM (const Matrix A, const Matrix B,
188  Matrix C, double m)
189 {
190  int i, j;
191  double V[3];
192  Matrix W;
193 
194  for (i = 0; i < 3; ++i)
195  {
196  for (j = 0; j < 3; ++j)
197  {
198  V[j] = B[j][i];
199  }
200  MatVecModM (A, V, V, m);
201  for (j = 0; j < 3; ++j)
202  {
203  W[j][i] = V[j];
204  }
205  }
206  for (i = 0; i < 3; ++i)
207  {
208  for (j = 0; j < 3; ++j)
209  {
210  C[i][j] = W[i][j];
211  }
212  }
213 }
214 
215 
216 //-------------------------------------------------------------------------
224 //
225 void MatTwoPowModM (const Matrix src, Matrix dst, double m, int32_t e)
226 {
227  int i, j;
228 
229  /* initialize: dst = src */
230  for (i = 0; i < 3; ++i)
231  {
232  for (j = 0; j < 3; ++j)
233  {
234  dst[i][j] = src[i][j];
235  }
236  }
237  /* Compute dst = src^(2^e) mod m */
238  for (i = 0; i < e; i++)
239  {
240  MatMatModM (dst, dst, dst, m);
241  }
242 }
243 
244 
245 //-------------------------------------------------------------------------
246 /*
254 //
255 void MatPowModM (const double A[3][3], double B[3][3], double m, int32_t n)
256 {
257  NS_LOG_FUNCTION (A << B << m << n);
258  int i, j;
259  double W[3][3];
260 
261  // initialize: W = A; B = I
262  for (i = 0; i < 3; ++i)
263  {
264  for (j = 0; j < 3; ++j)
265  {
266  W[i][j] = A[i][j];
267  B[i][j] = 0.0;
268  }
269  }
270  for (j = 0; j < 3; ++j)
271  {
272  B[j][j] = 1.0;
273  }
274 
275  // Compute B = A^n mod m using the binary decomposition of n
276  while (n > 0)
277  {
278  if (n % 2)
279  {
280  MatMatModM (W, B, B, m);
281  }
282  MatMatModM (W, W, W, m);
283  n /= 2;
284  }
285 }
286 */
287 
290 //
292 {
293  Matrix a1[190];
294  Matrix a2[190];
295 };
297 // raised to all powers of 2 from 1 to 191.
300 //
302 {
303  struct Precalculated precalculated;
304  for (int i = 0; i < 190; i++)
305  {
306  int power = i + 1;
307  MatTwoPowModM (A1p0, precalculated.a1[i], m1, power);
308  MatTwoPowModM (A2p0, precalculated.a2[i], m2, power);
309  }
310  return precalculated;
311 }
317 //
318 void PowerOfTwoMatrix (int n, Matrix a1p, Matrix a2p)
319 {
320  static struct Precalculated constants = PowerOfTwoConstants ();
321  for (int i = 0; i < 3; i ++)
322  {
323  for (int j = 0; j < 3; j++)
324  {
325  a1p[i][j] = constants.a1[n-1][i][j];
326  a2p[i][j] = constants.a2[n-1][i][j];
327  }
328  }
329 }
330 
331 } // end of anonymous namespace
332 
333 
334 namespace ns3 {
335 //-------------------------------------------------------------------------
336 // Generate the next random number.
337 //
339 {
340  int32_t k;
341  double p1, p2, u;
342 
343  /* Component 1 */
344  p1 = a12 * m_currentState[1] - a13n * m_currentState[0];
345  k = static_cast<int32_t> (p1 / m1);
346  p1 -= k * m1;
347  if (p1 < 0.0)
348  {
349  p1 += m1;
350  }
351  m_currentState[0] = m_currentState[1]; m_currentState[1] = m_currentState[2]; m_currentState[2] = p1;
352 
353  /* Component 2 */
354  p2 = a21 * m_currentState[5] - a23n * m_currentState[3];
355  k = static_cast<int32_t> (p2 / m2);
356  p2 -= k * m2;
357  if (p2 < 0.0)
358  {
359  p2 += m2;
360  }
361  m_currentState[3] = m_currentState[4]; m_currentState[4] = m_currentState[5]; m_currentState[5] = p2;
362 
363  /* Combination */
364  u = ((p1 > p2) ? (p1 - p2) * norm : (p1 - p2 + m1) * norm);
365 
366  return u;
367 }
368 
369 RngStream::RngStream (uint32_t seedNumber, uint64_t stream, uint64_t substream)
370 {
371  if (seedNumber >= m1 || seedNumber >= m2 || seedNumber == 0)
372  {
373  NS_FATAL_ERROR ("invalid Seed " << seedNumber);
374  }
375  for (int i = 0; i < 6; ++i)
376  {
377  m_currentState[i] = seedNumber;
378  }
379  AdvanceNthBy (stream, 127, m_currentState);
380  AdvanceNthBy (substream, 76, m_currentState);
381 }
382 
384 {
385  for (int i = 0; i < 6; ++i)
386  {
387  m_currentState[i] = r.m_currentState[i];
388  }
389 }
390 
391 void
392 RngStream::AdvanceNthBy (uint64_t nth, int by, double state[6])
393 {
394  Matrix matrix1,matrix2;
395  for (int i = 0; i < 64; i++)
396  {
397  int nbit = 63 - i;
398  int bit = (nth >> nbit) & 0x1;
399  if (bit)
400  {
401  PowerOfTwoMatrix(by + nbit, matrix1, matrix2);
402  MatVecModM (matrix1, state, state, m1);
403  MatVecModM (matrix2, &state[3], &state[3], m2);
404  }
405  }
406 }
407 
408 } // namespace ns3
NS_FATAL_x macro definitions.
const Matrix A1p0
First component transition matrix.
Definition: rng-stream.cc:90
const double a13n
First component multiplier of n - 3 value.
Definition: rng-stream.cc:70
double MultModM(double a, double s, double c, double m)
Return (a*s + c) MOD m; a, s, c and m must be < 2^35.
Definition: rng-stream.cc:118
const double a12
First component multiplier of n - 2 value.
Definition: rng-stream.cc:66
void MatMatModM(const Matrix A, const Matrix B, Matrix C, double m)
Compute the matrix C = A*B MOD m.
Definition: rng-stream.cc:187
Matrix a2[190]
Second component transition matrix powers.
Definition: rng-stream.cc:294
The transition matrices of the two MRG components (in matrix form), raised to all powers of 2 from 1 ...
Definition: rng-stream.cc:291
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
void MatVecModM(const Matrix A, const double s[3], double v[3], double m)
Compute the vector v = A*s MOD m.
Definition: rng-stream.cc:158
Combined Multiple-Recursive Generator MRG32k3a.
Definition: rng-stream.h:49
Matrix a1[190]
First component transition matrix powers.
Definition: rng-stream.cc:293
void MatTwoPowModM(const Matrix src, Matrix dst, double m, int32_t e)
Compute the matrix B = (A^(2^e) Mod m); works also if A = B.
Definition: rng-stream.cc:225
void AdvanceNthBy(uint64_t nth, int by, double state[6])
Advance state of the RNG by leaps and bounds.
Definition: rng-stream.cc:392
double m_currentState[6]
The RNG state vector.
Definition: rng-stream.h:85
const double a23n
Second component multiplier of n - 3 value.
Definition: rng-stream.cc:78
RngStream(uint32_t seed, uint64_t stream, uint64_t substream)
Construct from explicit seed, stream and substream values.
Definition: rng-stream.cc:369
const double norm
Normalization to obtain randoms on [0,1).
Definition: rng-stream.cc:62
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const double a21
Second component multiplier of n - 1 value.
Definition: rng-stream.cc:74
Declaration of class RngStream.
struct Precalculated PowerOfTwoConstants(void)
Compute the transition matrices of the two MRG components.
Definition: rng-stream.cc:301
double RandU01(void)
Generate the next random number for this stream.
Definition: rng-stream.cc:338
void PowerOfTwoMatrix(int n, Matrix a1p, Matrix a2p)
Get the transition matrices raised to a power of 2.
Definition: rng-stream.cc:318
const double m2
Second component modulus, 232 - 22853.
Definition: rng-stream.cc:58
const Matrix A2p0
Second component transition matrix.
Definition: rng-stream.cc:98
double Matrix[3][3]
Type for 3x3 matrix of doubles.
Definition: rng-stream.cc:50
Debug message logging.
const double m1
First component modulus, 232 - 209.
Definition: rng-stream.cc:54
const double two17
Decomposition factor for computing a*s in less than 53 bits, 217
Definition: rng-stream.cc:82
const double two53
IEEE-754 floating point precision, 253
Definition: rng-stream.cc:86