00001 00002 // 00003 // This file is part of the SHUFFLE program 00004 // written by Edward H. Trager and 00005 // Copyright (c) 2005 by the 00006 // Regents of the University of Michigan. 00007 // All Rights Reserved. 00008 // 00009 // The latest version of this program is available from: 00010 // 00011 // http://eyegene.ophthy.med.umich.edu/shuffle/ 00012 // 00013 // Released under the GNU General Public License. 00014 // A copy of the GPL is included in the distribution 00015 // package of this software, or see: 00016 // 00017 // http://www.gnu.org/copyleft/ 00018 // 00019 // ... for licensing details. 00020 // 00022 00023 // 00024 // Class RandomGenerator 00025 // 00026 // The random number generator presented here is based on 00027 // the "ran2" routine presented on page 282 of Press, Flannery, Teukolsky 00028 // and Vetterling's "Numerical Recipes in C" (c) 1992 by Cambridge 00029 // University Press (ISBN 0-521-43108-5). 00030 // 00031 // The routine implements the long-period (>2x10^18) random number generator 00032 // of L'Ecuyer with Bays-Durham shuffle and added safeguards. Returns a 00033 // random uniform deviate between 0.0 and 1.0 (exclusive of the endpoint values). 00034 // RNMX should approximate the largest floating value that is less than 1. 00035 // 00036 // I have added the feature of gathering entropy from either 00037 // /dev/random or /dev/urandom in order to get a starting seed. 00038 // 00039 #include <fstream> 00040 #include <iostream> 00041 00042 #define defined_EPS 3.0e-16 00043 #define defined_RNMX (1.0 - defined_EPS ) 00044 00045 #ifndef RANDOM_GENERATOR_INCLUDED 00046 00047 class RandomGenerator { 00048 00049 public: 00050 00051 // 00052 // Used for entropy seed: 00053 // TRUE_RANDOM grabs from /dev/random 00054 // PSEUDO_RANDOM grabs from /dev/urandom 00055 // 00056 enum ENTROPY_TYPE { TRUE_RANDOM, PSEUDO_RANDOM, UNSPECIFIED_RANDOM }; 00057 00058 private: 00059 00060 ENTROPY_TYPE _seedSource; 00061 00062 const static int IM1 = 2147483563; 00063 const static int IM2 = 2147483399; 00064 const static int IMM1 = IM1-1; 00065 const static int IA1 = 40014; 00066 const static int IA2 = 40692; 00067 const static int IQ1 = 53668; 00068 const static int IQ2 = 52774; 00069 const static int IR1 = 12211; 00070 const static int IR2 = 3791; 00071 const static int NTAB = 32; 00072 const static int NDIV = 1+IMM1/NTAB; 00073 00074 //const static double EPS = defined_EPS; 00075 //const static double RNMX = defined_RNMX; 00076 //const static double AM = 1.0/IM1; 00077 const static double EPS; 00078 const static double RNMX; 00079 const static double AM; 00080 00081 int _idum; 00082 int _idum2; 00083 int iy; 00084 int iv[NTAB]; 00085 int j; 00086 int k; 00087 double temp; 00088 00089 // 00090 // Private method to gather entropy: 00091 // 00092 void _gatherEntropy(); 00093 00094 // 00095 // Private method to initialize the random number generator: 00096 // 00097 void _initialize(void); 00098 00099 public: 00100 00101 // 00102 // Constructor: 00103 // 00104 // 2006.08.10.ET: Use "PSEUDO_RANDOM" by default: 00105 // 00106 RandomGenerator( ENTROPY_TYPE seedSource = PSEUDO_RANDOM ); 00107 00108 // 00109 // setEntropySource() is used in the case that the constructor 00110 // for the object had seedSource=UNSPECIFIED_RANDOM: 00111 // 00112 void setEntropySource(ENTROPY_TYPE seedSource); 00113 00114 // 00115 // reset(): Resets the random generator with a new seed 00116 // 00117 void reset( void ); 00118 00119 // 00120 // getDeviate() 00121 // 00122 // Returns a number greater than or equal to 0.0 00123 // and less than 1.0 (*not* less than or equal): 00124 // 00125 double getDeviate( void ); 00126 00127 // 00128 // getIntegerInRange 00129 // 00130 // Returns an integer in the range from low *inclusive* 00131 // to high *inclusive*: 00132 // 00133 int getIntegerInRange( int low, int high); 00134 00135 }; 00136 00137 #define RANDOM_GENERATOR_INCLUDED 00138 #endif