root / include / clcg32.h @ fbe00e73
History  View  Annotate  Download (2.88 KB)
1 
//==========================================================================


2 
// CLCG32.CC  part of

3 
// OMNeT++/OMNEST

4 
// Discrete System Simulation in C++

5 
//

6 
// Contents:

7 
// class cLCG32

8 
//

9 
//==========================================================================

10  
11 
/**

12 
Copyright (C) 20022008 Andras Varga

13 
Copyright (C) 20062008 OpenSim Ltd.

14 

15 
This file is distributed WITHOUT ANY WARRANTY. See the file

16 
`license' for details on this and other legal matters.

17 
**/

18  
19 
#ifndef __CLCG32_H

20 
#define __CLCG32_H

21  
22 
#include "simkerneldefs.h" 
23 
#include "globals.h" 
24 
#include "crng.h" 
25 
#include "cconfiguration.h" 
26  
27 
NAMESPACE_BEGIN 
28  
29  
30 
#define LCG32_MAX 0x7ffffffeL /* = 2^312 */ 
31  
32 
/**

33 
* Implements a 32bit (2^312 cycle length) linear congruential random

34 
* number generator.

35 
*  Range: 1 ... 2^312

36 
*  Period length: 2^312

37 
*  Method: x=(x * 7^5) mod (2^311)

38 
*  Required hardware: exactly 32bit integer aritmetics

39 
*  To check: if x[0]=1 then x[10000]=1,043,618,065

40 
*

41 
* Source: Raj Jain: The Art of Computer Systems Performance Analysis

42 
* (John Wiley & Sons, 1991) pp 441444, 455.

43 
*/

44 
class SIM_API cLCG32 : public cRNG 
45 
{ 
46 
protected:

47 
long seedValue;

48  
49 
// 256 pregenerated seeds, spaced 8,388,608 values in the sequence.

50 
// This covers the whole RNG period. Enough for 128 runs with 2 RNGs

51 
// each, or 64 runs with 4 RNGs each  assuming one run never uses

52 
// more than 8 million random numbers per RNG.

53 
static long autoSeeds[256]; 
54  
55 
public:

56 
cLCG32() {} 
57 
virtual ~cLCG32() {} 
58  
59  
60 
/**

61 
* Called by the simulation framework to set up this RNG as the central

62 
* seed generating RNG.

63 
*/

64 
virtual void initializeAsMaster(int runNumber, int rngId, int numRngs, 
65 
int parsimProcId, int parsimNumPartitions, cConfiguration *cfg); 
66  
67 
/** Sets up the RNG. */

68 
virtual void initialize(int seedSet, int rngId, int numRngs, 
69 
int parsimProcId, int parsimNumPartitions, 
70 
cConfiguration *cfg); 
71  
72 
/** Tests correctness of the RNG */

73 
virtual void selfTest();

74  
75 
/** Random integer in the range [0,intRandMax()] */

76 
virtual unsigned long intRand(); 
77  
78 
/** Maximum value that can be returned by intRand() */

79 
virtual unsigned long intRandMax(); 
80  
81 
/** Random integer in [0,n), n < intRandMax() */

82 
virtual unsigned long intRand(unsigned long n); 
83  
84 
/** Random double on the [0,1) interval */

85 
virtual double doubleRand();

86  
87 
/** Random double on the (0,1) interval */

88 
virtual double doubleRandNonz();

89  
90 
/** Random double on the [0,1] interval */

91 
virtual double doubleRandIncl1();

92  
93 
/** set seed without using initialize */

94 
virtual void seed(unsigned long seed); 
95 
}; 
96  
97 
NAMESPACE_END 
98  
99  
100 
#endif

101  
102 