openModeller
Version 1.4.0
|
00001 00029 #include <openmodeller/Random.hh> 00030 #include <openmodeller/os_specific.hh> 00031 00032 #include <stdlib.h> 00033 00034 00035 /********************************************************/ 00036 /************************ Random ************************/ 00037 00038 int Random::_initialized = 0; 00039 00040 00041 /*******************/ 00042 /*** Constructor ***/ 00043 00044 Random::Random() 00045 { 00046 if ( ! _initialized ) 00047 _initialized = initRandom(); 00048 } 00049 00050 00051 /********************/ 00052 /*** get (double) ***/ 00053 double 00054 Random::get( double min, double max ) 00055 { 00056 return( (max-min) * random() + min ); 00057 } 00058 00059 double 00060 Random::get( double max ) 00061 { 00062 return( max * random() ); 00063 } 00064 00065 00066 /***************************/ 00067 /*** operator() (double) ***/ 00068 double 00069 Random::operator()( double min, double max ) 00070 { 00071 return( (max-min) * random() + min ); 00072 } 00073 00074 double 00075 Random::operator()( double max ) 00076 { 00077 return( max * random() ); 00078 } 00079 00080 00081 /*****************/ 00082 /*** get (int) ***/ 00083 int 00084 Random::get( int min, int max ) 00085 { 00086 return( int((max-min) * random()) + min ); 00087 } 00088 00089 int 00090 Random::get( int max ) 00091 { 00092 return( int(max * random()) ); 00093 } 00094 00095 00096 /************************/ 00097 /*** operator() (int) ***/ 00098 int 00099 Random::operator()( int min, int max ) 00100 { 00101 return( int((max-min) * random()) + min ); 00102 } 00103 00104 int 00105 Random::operator()( int max ) 00106 { 00107 return( int(max * random()) ); 00108 } 00109 00110 00111 /*******************/ 00112 /*** get (long) ***/ 00113 long 00114 Random::get( long min, long max ) 00115 { 00116 return( long((max-min) * random()) + min ); 00117 } 00118 00119 long 00120 Random::get( long max ) 00121 { 00122 return( long(max * random()) ); 00123 } 00124 00125 00126 /**************************/ 00127 /*** operator() (long) ***/ 00128 long 00129 Random::operator()( long min, long max ) 00130 { 00131 return( long((max-min) * random()) + min ); 00132 } 00133 00134 long 00135 Random::operator()( long max ) 00136 { 00137 return( long(max * random()) ); 00138 } 00139 00140 00141 /****************/ 00142 /*** discrete ***/ 00143 double 00144 Random::discrete( float range, float dim_interv ) 00145 { 00146 double d = 2.0 * range / dim_interv; 00147 int k = int( (d + 1) * random() ); 00148 return( k * dim_interv - range ); 00149 } 00150 00151 00152 /**************/ 00153 /*** random ***/ 00154 double 00155 Random::random() 00156 { 00157 return ::rand() / (RAND_MAX + 1.0); 00158 } 00159 00160