Statistics
| Branch: | Revision:

root / include / crng.h @ master

History | View | Annotate | Download (3.59 KB)

1
//==========================================================================
2
//   CRNG.H  - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  cRNG: interface for random number generator classes
8
//
9
//==========================================================================
10

    
11
/*--------------------------------------------------------------*
12
  Copyright (C) 1992-2008 Andras Varga
13
  Copyright (C) 2006-2008 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 __CRNG_H
20
#define __CRNG_H
21

    
22
#include "simkerneldefs.h"
23
#include "cobject.h"
24
#include "cconfiguration.h"
25
#include "cconfigoption.h"
26

    
27
NAMESPACE_BEGIN
28

    
29
class cConfiguration;
30

    
31

    
32
/**
33
 * Abstract interface for random number generator classes.
34
 * Some known implementations are <tt>cMersenneTwister</tt>,
35
 * <tt>cLCG32</tt> and <tt>cAkaroaRNG</tt>. The actual RNG class
36
 * to be used in simulations can be configured (a feature of the
37
 * Envir library).
38
 *
39
 * To switch to your own RNG, subclass cRNG, register your new class
40
 * with the Register_Class() macro, then select it
41
 * by adding the following to <tt>omnetpp.ini</tt>:
42
 *
43
 * <pre>
44
 * [General]
45
 * rng-class="MyClass"
46
 * </pre>
47
 *
48
 * See also cModule::getRNG().
49
 *
50
 * @ingroup RandomNumbers
51
 * @ingroup EnvirExtensions
52
 */
53
class SIM_API cRNG : public cObject
54
{
55
  protected:
56
    unsigned long numDrawn;
57

    
58
  public:
59
    cRNG() {numDrawn=0;}
60
    virtual ~cRNG() {}
61

    
62
          /**
63
         * Called by the simulation framework to set up this RNG as the central
64
         * seed generating RNG.
65
         */
66
        virtual void initializeAsMaster(int runNumber, int rngId, int numRngs,
67
                        int parsimProcId, int parsimNumPartitions, cConfiguration *cfg) = 0;
68

    
69
    /**
70
     * Called by the simulation framework to set up the RNG at the start
71
     * of a simulation run. Seed selection should take place here.
72
     */
73
    virtual void initialize(int seedSet, int rngId, int numRngs,
74
                            int parsimProcId, int parsimNumPartitions,
75
                            cConfiguration *cfg) = 0;
76

    
77
    /**
78
     * Coarse test for the correctness of the RNG algorithm. It should detect
79
     * platform-dependent bugs (e.g. caused by different word size or compiler
80
     * anomaly). The method should throw an exception if something is wrong.
81
     * It is invoked only once, when the simulation program starts up.
82
     */
83
    virtual void selfTest() = 0;
84

    
85
    /**
86
     * Returns how many random numbers have been drawn from this RNG.
87
     * Subclasses should increment numDrawn in the intRand(), etc. methods.
88
     */
89
    virtual unsigned long getNumbersDrawn() const  {return numDrawn;}
90

    
91
    /**
92
     * Random integer in the range [0,intRandMax()]
93
     */
94
    virtual unsigned long intRand() = 0;
95

    
96
    /**
97
     * Maximum value that can be returned by intRand(), e.g. 2^31-2 with LCG32.
98
     */
99
    virtual unsigned long intRandMax() = 0;
100

    
101
    /**
102
     * Random integer in [0,n), n < intRandMax()
103
     */
104
    virtual unsigned long intRand(unsigned long n) = 0;
105

    
106
    /**
107
     * Random double on the [0,1) interval
108
     */
109
    virtual double doubleRand() = 0;
110

    
111
    /**
112
     * Random double on the (0,1) interval
113
     */
114
    virtual double doubleRandNonz() = 0;
115

    
116
    /**
117
     * Random double on the [0,1] interval
118
     */
119
    virtual double doubleRandIncl1() = 0;
120

    
121
    /**
122
     * Random double on the (0,1] interval
123
     */
124
    double doubleRandNonzIncl1() {return 1-doubleRand();}
125

    
126
    /** set seed without using initialize */
127
    virtual void seed(unsigned long seed) = 0;
128
};
129

    
130
NAMESPACE_END
131

    
132

    
133
#endif
134