ThreeB 1.1
randomc.h
Go to the documentation of this file.
00001 /*****************************   randomc.h   **********************************
00002 * Author:        Agner Fog
00003 * Date created:  1997
00004 * Last modified: 2008-11-16
00005 * Project:       randomc.h
00006 * Source URL:    www.agner.org/random
00007 *
00008 * Description:
00009 * This header file contains class declarations and other definitions for the 
00010 * randomc class library of uniform random number generators in C++ language.
00011 *
00012 * Overview of classes:
00013 * ====================
00014 *
00015 * class CRandomMersenne:
00016 * Random number generator of type Mersenne twister.
00017 * Source file mersenne.cpp
00018 *
00019 * class CRandomMother:
00020 * Random number generator of type Mother-of-All (Multiply with carry).
00021 * Source file mother.cpp
00022 *
00023 * class CRandomSFMT:
00024 * Random number generator of type SIMD-oriented Fast Mersenne Twister.
00025 * The class definition is not included here because it is not
00026 * portable to all platforms. See sfmt.h and sfmt.cpp for details.
00027 *
00028 * Member functions (methods):
00029 * ===========================
00030 *
00031 * All these classes have identical member functions:
00032 *
00033 * Constructor(int seed):
00034 * The seed can be any integer. The time may be used as seed.
00035 * Executing a program twice with the same seed will give the same sequence 
00036 * of random numbers. A different seed will give a different sequence.
00037 *
00038 * void RandomInit(int seed);
00039 * Re-initializes the random number generator with a new seed.
00040 *
00041 * void RandomInitByArray(int const seeds[], int NumSeeds);
00042 * In CRandomMersenne and CRandomSFMT only: Use this function if you want 
00043 * to initialize with a seed with more than 32 bits. All bits in the seeds[]
00044 * array will influence the sequence of random numbers generated. NumSeeds 
00045 * is the number of entries in the seeds[] array.
00046 *
00047 * double Random();
00048 * Gives a floating point random number in the interval 0 <= x < 1.
00049 * The resolution is 32 bits in CRandomMother and CRandomMersenne, and
00050 * 52 bits in CRandomSFMT.
00051 *
00052 * int IRandom(int min, int max);
00053 * Gives an integer random number in the interval min <= x <= max.
00054 * (max-min < MAXINT).
00055 * The precision is 2^-32 (defined as the difference in frequency between 
00056 * possible output values). The frequencies are exact if max-min+1 is a
00057 * power of 2.
00058 *
00059 * int IRandomX(int min, int max);
00060 * Same as IRandom, but exact. In CRandomMersenne and CRandomSFMT only.
00061 * The frequencies of all output values are exactly the same for an 
00062 * infinitely long sequence. (Only relevant for extremely long sequences).
00063 *
00064 * uint32_t BRandom();
00065 * Gives 32 random bits. 
00066 *
00067 *
00068 * Example:
00069 * ========
00070 * The file EX-RAN.CPP contains an example of how to generate random numbers.
00071 *
00072 *
00073 * Library version:
00074 * ================
00075 * Optimized versions of these random number generators are provided as function
00076 * libraries in randoma.zip. These function libraries are coded in assembly
00077 * language and support only x86 platforms, including 32-bit and 64-bit
00078 * Windows, Linux, BSD, Mac OS-X (Intel based). Use randoma.h from randoma.zip
00079 *
00080 *
00081 * Non-uniform random number generators:
00082 * =====================================
00083 * Random number generators with various non-uniform distributions are 
00084 * available in stocc.zip (www.agner.org/random).
00085 *
00086 *
00087 * Further documentation:
00088 * ======================
00089 * The file ran-instructions.pdf contains further documentation and 
00090 * instructions for these random number generators.
00091 *
00092 * Copyright 1997-2008 by Agner Fog. 
00093 * GNU General Public License http://www.gnu.org/licenses/gpl.html
00094 *******************************************************************************/
00095 
00096 #ifndef RANDOMC_H
00097 #define RANDOMC_H
00098 
00099 // Define integer types with known size: int32_t, uint32_t, int64_t, uint64_t.
00100 // If this doesn't work then insert compiler-specific definitions here:
00101 #if defined(__GNUC__)
00102   // Compilers supporting C99 or C++0x have inttypes.h defining these integer types
00103   #include <inttypes.h>
00104   #define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
00105 #elif defined(_WIN16) || defined(__MSDOS__) || defined(_MSDOS) 
00106    // 16 bit systems use long int for 32 bit integer
00107   typedef   signed long int int32_t;
00108   typedef unsigned long int uint32_t;
00109 #elif defined(_MSC_VER)
00110   // Microsoft have their own definition
00111   typedef   signed __int32  int32_t;
00112   typedef unsigned __int32 uint32_t;
00113   typedef   signed __int64  int64_t;
00114   typedef unsigned __int64 uint64_t;
00115   #define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
00116 #else
00117   // This works with most compilers
00118   typedef signed int          int32_t;
00119   typedef unsigned int       uint32_t;
00120   typedef long long           int64_t;
00121   typedef unsigned long long uint64_t;
00122   #define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
00123 #endif
00124 
00125 
00126 /***********************************************************************
00127 System-specific user interface functions
00128 ***********************************************************************/
00129 
00130 void EndOfProgram(void);               // System-specific exit code (userintf.cpp)
00131 
00132 void FatalError(const char *ErrorText);// System-specific error reporting (userintf.cpp)
00133 
00134 #if defined(__cplusplus)               // class definitions only in C++
00135 /***********************************************************************
00136 Define random number generator classes
00137 ***********************************************************************/
00138 
00139 class CRandomMersenne {                // Encapsulate random number generator
00140 // Choose which version of Mersenne Twister you want:
00141 #if 0 
00142 // Define constants for type MT11213A:
00143 #define MERS_N   351
00144 #define MERS_M   175
00145 #define MERS_R   19
00146 #define MERS_U   11
00147 #define MERS_S   7
00148 #define MERS_T   15
00149 #define MERS_L   17
00150 #define MERS_A   0xE4BD75F5
00151 #define MERS_B   0x655E5280
00152 #define MERS_C   0xFFD58000
00153 #else    
00154 // or constants for type MT19937:
00155 #define MERS_N   624
00156 #define MERS_M   397
00157 #define MERS_R   31
00158 #define MERS_U   11
00159 #define MERS_S   7
00160 #define MERS_T   15
00161 #define MERS_L   18
00162 #define MERS_A   0x9908B0DF
00163 #define MERS_B   0x9D2C5680
00164 #define MERS_C   0xEFC60000
00165 #endif
00166 
00167 public:
00168    CRandomMersenne(int seed) {         // Constructor
00169       RandomInit(seed); LastInterval = 0;}
00170    void RandomInit(int seed);          // Re-seed
00171    void RandomInitByArray(int const seeds[], int NumSeeds); // Seed by more than 32 bits
00172    int IRandom (int min, int max);     // Output random integer
00173    int IRandomX(int min, int max);     // Output random integer, exact
00174    double Random();                    // Output random float
00175    uint32_t BRandom();                 // Output random bits
00176 
00177    uint32_t* get_state()
00178    {
00179         return mt;
00180    }
00181 
00182    int& get_index()
00183    {
00184         return mti;
00185    }
00186 private:
00187    void Init0(int seed);               // Basic initialization procedure
00188    uint32_t mt[MERS_N];                // State vector
00189    int mti;                            // Index into mt
00190    uint32_t LastInterval;              // Last interval length for IRandomX
00191    uint32_t RLimit;                    // Rejection limit used by IRandomX
00192 };    
00193 
00194 
00195 class CRandomMother {                  // Encapsulate random number generator
00196 public:
00197    void RandomInit(int seed);          // Initialization
00198    int IRandom(int min, int max);      // Get integer random number in desired interval
00199    double Random();                    // Get floating point random number
00200    uint32_t BRandom();                 // Output random bits
00201    CRandomMother(int seed) {           // Constructor
00202       RandomInit(seed);}
00203 protected:
00204    uint32_t x[5];                      // History buffer
00205 };
00206 
00207 #endif // __cplusplus
00208 #endif // RANDOMC_H