00001 #include <iostream>
00002 #include <time.h>
00003 #include "GLLogicError.h"
00004 #include "GLRandomNumbersGenerator.h"
00005
00006 bool GLRandomNumbersGenerator::m_is_initialised = false;
00007 long GLRandomNumbersGenerator::m_initial_seed = UNINITIALISED_SEED;
00008
00009
00014 long GLRandomNumbersGenerator::getRandomInteger()
00015 {
00016 if (!m_is_initialised) initGenerator();
00017 return rand();
00018 }
00019
00025 long GLRandomNumbersGenerator::getRandomInteger(
00026 const long& max)
00027 {
00028
00029 if (max <= 0)
00030 {
00031 char errormsg[1000];
00032 sprintf(errormsg, "GLRandomNumbersGenerator::getRandomInteger"
00033 "Max should be > 0 while %li was provided",
00034 max);
00035 throw GLLogicError(errormsg);
00036 }
00037 if (max > getMaxNumber())
00038 {
00039 char errormsg[1000];
00040 sprintf(errormsg, "GLRandomNumbersGenerator::getRandomInteger"
00041 "Max should be < %li while %li was provided",
00042 getMaxNumber(), max);
00043 throw GLLogicError(errormsg);
00044 }
00045 long int r = getRandomInteger();
00046 return (r % max);
00047 }
00048
00055 long GLRandomNumbersGenerator::getRandomInteger(
00056 const long& min, const long& max)
00057 {
00058
00059 if (min >= max)
00060 {
00061 char errormsg[1000];
00062 sprintf(errormsg, "GLRandomNumbersGenerator::getRandomInteger"
00063 "min (=%li) should be < max (= %li)\n",
00064 min, max);
00065 throw GLLogicError(errormsg);
00066 }
00067 return getRandomInteger(max - min) + min;
00068 }
00069
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00126 double GLRandomNumbersGenerator::getRandomUniform(
00127 const double& min,
00128 const double& max)
00129 {
00130
00131 if (min >= max)
00132 {
00133 char errormsg[1000];
00134 sprintf(errormsg, "GLRandomNumbersGenerator::getRandomUniform"
00135 "min (=%e) should be < max (= %e)\n",
00136 min, max);
00137 throw GLLogicError(errormsg);
00138 }
00139 double length = max - min;
00140 return min + length *
00141 double(getRandomInteger()) / double(getMaxNumber());
00142 }
00143
00153 long GLRandomNumbersGenerator::initGenerator(long seed)
00154 {
00155 if (seed != UNINITIALISED_SEED) m_initial_seed = seed;
00156 else m_initial_seed = time(NULL);
00157 cout << "Initialising the random generator with "
00158 << m_initial_seed << endl;
00159 srand(m_initial_seed);
00160 m_is_initialised = true;
00161 return m_initial_seed;
00162 }
00163
00164
00172 void GLRandomNumbersGenerator::shuffleIntegerVector(
00173 vector<int>& result,
00174 const long &min,
00175 const long &max)
00176 {
00177
00178 if (min >= max)
00179 {
00180 char errormsg[1000];
00181 sprintf(errormsg, "GLRandomNumbersGenerator::"
00182 "GLRandomNumbersGenerator::shuffleIntegerVector"
00183 "min (=%li) should be < max (= %li)\n",
00184 min, max);
00185 throw GLLogicError(errormsg);
00186 }
00187
00188 int size = max - min;
00189 int size_m1 = size - 1;
00190 result.clear();
00191 result.resize(size);
00192 for(int i = min, j = 0; i < max; i++, j++)
00193 {
00194 result[j] = i;
00195 }
00196
00197 for(int i = 0; i < size_m1; i++)
00198 {
00199
00200
00201 int j = getRandomInteger(i, size);
00202
00203 if (i == j) continue;
00204 int tmp = result[j];
00205 result[j] = result[i];
00206 result[i] = tmp;
00207 }
00208 }