00001 00020 #include "random.h" 00021 #include <iostream> 00022 #include <math.h> 00023 #include <cassert> 00024 00025 using namespace std; 00026 00027 Random::Random(IRealRandom *random) { 00028 assert(random != NULL); 00029 this->random = random; 00030 } 00031 00032 Random::~Random(void) { 00033 delete this->random; 00034 } 00035 00036 00037 const double PI=3.1415926; 00038 00039 double Random::normal(double desv) { 00040 double u1, u2; 00041 double result; 00042 00043 do { 00044 u1=Random::rand(); 00045 } while (u1 == 0); 00046 00047 u2=Random::rand(); 00048 result = desv * sqrt (-2*log(u1)) * sin (2*PI*u2); 00049 00050 return result; 00051 } 00052 00053 void initSample(int *sample, int max) { 00054 int i; 00055 00056 for (i = 0; i < max; i++) { 00057 sample[i] = i; 00058 } 00059 } 00060 00061 int Random::getSample(int *sample, int *pmax) { 00062 int max = *pmax; 00063 int r, pos; 00064 00065 assert(max >= 0); 00066 r = randint(0, max-1); 00067 pos = sample[r]; 00068 sample[r] = sample[max-1]; 00069 --max; 00070 00071 *pmax = max; 00072 return pos; 00073 } 00074