00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef CVD_RANDOM_H
00022 #define CVD_RANDOM_H
00023
00024 #include <cstdlib>
00025 #include <cmath>
00026
00027 namespace CVD {
00032 inline double rand_u()
00033 {
00034 return ((double) std::rand()/ RAND_MAX);
00035 }
00036
00041 inline double rand_g()
00042 {
00043 static bool use_old=false;
00044 static double y2;
00045 double r;
00046
00047
00048 if(!use_old)
00049 {
00050 double x1, x2, w, y1;
00051 do {
00052 x1 = 2.0 * rand_u() - 1.0;
00053 x2 = 2.0 * rand_u() - 1.0;
00054 w = x1 * x1 + x2 * x2;
00055 } while ( w >= 1.0 );
00056
00057 w = std::sqrt( (-2.0 * std::log( w ) ) / w );
00058 y1 = x1 * w;
00059 y2 = x2 * w;
00060
00061 r = y1;
00062 use_old = true;
00063 }
00064 else
00065 {
00066 r = y2;
00067 use_old = false;
00068 }
00069
00070
00071 return r;
00072 }
00073
00074 }
00075
00076 #endif