LCOV - code coverage report
Current view: top level - util/cgm - RandomMersenne.hpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 5 5 100.0 %
Date: 2020-06-30 00:58:45 Functions: 2 2 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : //-------------------------------------------------------------------------
       2                 :            : // Filename      : RandomMersenne.hpp 
       3                 :            : //
       4                 :            : // Purpose       : Main include for random number generator
       5                 :            : //                 This random number generator came from the authors of the Mersenne
       6                 :            : //                 random number generator, Makoto Matsumoto and Takuji Nishimura.
       7                 :            : //                 See the definition file RandomMersenne.cpp for the full copyright
       8                 :            : //                 statement.
       9                 :            : //
      10                 :            : //                 I have made the following changes, starting from the (faster version
      11                 :            : //                 of the) source code provided by the authors.  I have made the following
      12                 :            : //                 changes:
      13                 :            : //                 - put the generator into a C++ class, to simplify use of inline functions
      14                 :            : //                   and member instead of global variables
      15                 :            : //                 - made the period parameters enum variables instead of #defines,
      16                 :            : //                   formerly static state variables member variables, and formerly
      17                 :            : //                   global functions member functions.
      18                 :            : //                 - removed check for initialization from next_state, since I require
      19                 :            : //                   initialization when the class object is created
      20                 :            : //
      21                 :            : //                 Other Notes:
      22                 :            : //                 - I made the no-argument constructor private, to force input of
      23                 :            : //                   a seed value.  The random sequence gets initialized upon construction.
      24                 :            : //                 - function names in this class don't follow our naming convention, because
      25                 :            : //                   I wanted to stay as close to the original source code as possible
      26                 :            : //                 - most of the public functions in this class are inlined, in the hopes that
      27                 :            : //                   at least some compilers will honor that; these functions all call a
      28                 :            : //                   non-inlined function next_state() though.
      29                 :            : //
      30                 :            : //
      31                 :            : // Creator       : Tim Tautges (mostly code from aforementioned site)
      32                 :            : //
      33                 :            : // Date          : 08/02
      34                 :            : //
      35                 :            : // Owner         : Tim Tautges
      36                 :            : //-------------------------------------------------------------------------
      37                 :            : 
      38                 :            : #ifndef RANDOM_MERSENNE_HPP
      39                 :            : #define RANDOM_MERSENNE_HPP
      40                 :            : 
      41                 :            : #include "CGMUtilConfigure.h"
      42                 :            : 
      43                 :            : class CUBIT_UTIL_EXPORT RandomMersenne {                  // encapsulate random number generator
      44                 :            : 
      45                 :            : public:
      46                 :            : 
      47                 :            :     //! constructor initialized random number generator, so it takes a seed
      48                 :            :   RandomMersenne(unsigned long seed);
      49                 :            : 
      50                 :            :     //! constructor initialized random number generator, so it takes a seed array
      51                 :            :   RandomMersenne(unsigned long init_key[], unsigned long key_length);
      52                 :            : 
      53                 :            :     //! initialize by an array with array-length
      54                 :            :     //! init_key is the array for initializing keys
      55                 :            :     //! key_length is its length
      56                 :            :   void init_by_array(unsigned long init_key[], unsigned long key_length);
      57                 :            : 
      58                 :            :     //! generates a random number on [0,0xffffffff]-interval 
      59                 :            :   unsigned long genrand_int32();
      60                 :            :   
      61                 :            :     //! generates a random number on [0,0x7fffffff]-interval 
      62                 :            :   long genrand_int31();
      63                 :            : 
      64                 :            :     //! generates a random number on [0,1]-real-interval 
      65                 :            :   double genrand_real1();
      66                 :            : 
      67                 :            :     //! generates a random number on [0,1)-real-interval 
      68                 :            :   double genrand_real2();
      69                 :            : 
      70                 :            :     //! generates a random number on (0,1)-real-interval 
      71                 :            :   double genrand_real3();
      72                 :            : 
      73                 :            :     //! generates a random number on [0,1) with 53-bit resolution
      74                 :            :   double genrand_res53();
      75                 :            : 
      76                 :            : private:
      77                 :            : 
      78                 :            :     //! private constructor to prevent constructing without seed
      79                 :            :   RandomMersenne() {}
      80                 :            : 
      81                 :            :     //! initializes state[N] with a seed
      82                 :            :   void init_genrand(unsigned long s);
      83                 :            : 
      84                 :            :   enum {
      85                 :            :     N = 624, M = 397,
      86                 :            :     MATRIX_A = 0x9908b0dfU, //! constant vector a    
      87                 :            :     UPPER_MASK = 0x80000000U, /* most significant w-r bits */
      88                 :            :     LOWER_MASK = 0x7fffffffU /* least significant r bits */
      89                 :            :   };
      90                 :            : 
      91                 :            :   unsigned long mt[N]; //! the array for the state vector  
      92                 :            :   unsigned mti;
      93                 :            :   int left;
      94                 :            :   int initf;
      95                 :            :   unsigned long *next;
      96                 :            : 
      97                 :            : };    
      98                 :            : 
      99                 :         43 : inline RandomMersenne::RandomMersenne(unsigned long seed) 
     100                 :            : {
     101                 :         43 :   init_genrand(seed);
     102                 :         43 : }
     103                 :            : 
     104                 :            : inline RandomMersenne::RandomMersenne(unsigned long init_key[], 
     105                 :            :                                       unsigned long key_length)
     106                 :            : {
     107                 :            :   init_by_array(init_key, key_length);
     108                 :            : }
     109                 :            : 
     110                 :            : /* generates a random number on [0,0x7fffffff]-interval */
     111                 :        954 : inline long RandomMersenne::genrand_int31()
     112                 :            : {
     113                 :        954 :   return (long) (genrand_int32() >> 1);
     114                 :            : }
     115                 :            : 
     116                 :            : #endif  
     117                 :            : 

Generated by: LCOV version 1.11