cgma
|
00001 //------------------------------------------------------------------------- 00002 // Filename : RandomMersenne.hpp 00003 // 00004 // Purpose : Main include for random number generator 00005 // This random number generator came from the authors of the Mersenne 00006 // random number generator, Makoto Matsumoto and Takuji Nishimura. 00007 // See the definition file RandomMersenne.cpp for the full copyright 00008 // statement. 00009 // 00010 // I have made the following changes, starting from the (faster version 00011 // of the) source code provided by the authors. I have made the following 00012 // changes: 00013 // - put the generator into a C++ class, to simplify use of inline functions 00014 // and member instead of global variables 00015 // - made the period parameters enum variables instead of #defines, 00016 // formerly static state variables member variables, and formerly 00017 // global functions member functions. 00018 // - removed check for initialization from next_state, since I require 00019 // initialization when the class object is created 00020 // 00021 // Other Notes: 00022 // - I made the no-argument constructor private, to force input of 00023 // a seed value. The random sequence gets initialized upon construction. 00024 // - function names in this class don't follow our naming convention, because 00025 // I wanted to stay as close to the original source code as possible 00026 // - most of the public functions in this class are inlined, in the hopes that 00027 // at least some compilers will honor that; these functions all call a 00028 // non-inlined function next_state() though. 00029 // 00030 // 00031 // Creator : Tim Tautges (mostly code from aforementioned site) 00032 // 00033 // Date : 08/02 00034 // 00035 // Owner : Tim Tautges 00036 //------------------------------------------------------------------------- 00037 00038 #ifndef RANDOM_MERSENNE_HPP 00039 #define RANDOM_MERSENNE_HPP 00040 00041 #include "CGMUtilConfigure.h" 00042 00043 class CUBIT_UTIL_EXPORT RandomMersenne { // encapsulate random number generator 00044 00045 public: 00046 00048 RandomMersenne(unsigned long seed); 00049 00051 RandomMersenne(unsigned long init_key[], unsigned long key_length); 00052 00056 void init_by_array(unsigned long init_key[], unsigned long key_length); 00057 00059 unsigned long genrand_int32(); 00060 00062 long genrand_int31(); 00063 00065 double genrand_real1(); 00066 00068 double genrand_real2(); 00069 00071 double genrand_real3(); 00072 00074 double genrand_res53(); 00075 00076 private: 00077 00079 RandomMersenne() {} 00080 00082 void init_genrand(unsigned long s); 00083 00084 enum { 00085 N = 624, M = 397, 00086 MATRIX_A = 0x9908b0dfU, 00087 UPPER_MASK = 0x80000000U, /* most significant w-r bits */ 00088 LOWER_MASK = 0x7fffffffU /* least significant r bits */ 00089 }; 00090 00091 unsigned long mt[N]; 00092 unsigned mti; 00093 int left; 00094 int initf; 00095 unsigned long *next; 00096 00097 }; 00098 00099 inline RandomMersenne::RandomMersenne(unsigned long seed) 00100 { 00101 init_genrand(seed); 00102 } 00103 00104 inline RandomMersenne::RandomMersenne(unsigned long init_key[], 00105 unsigned long key_length) 00106 { 00107 init_by_array(init_key, key_length); 00108 } 00109 00110 /* generates a random number on [0,0x7fffffff]-interval */ 00111 inline long RandomMersenne::genrand_int31() 00112 { 00113 return (long) (genrand_int32() >> 1); 00114 } 00115 00116 #endif 00117