00001 00002 00005 00006 #include <cstdlib> 00007 #include <math.h> 00008 #include <art_map/gaussian.h> 00009 00010 inline float real_random(float multi=1.0){ 00011 return float(random())/RAND_MAX*multi; 00012 } 00013 00014 00021 gaussian::gaussian() { 00022 _mean1=_var1=_std1=0; 00023 _ready=false; 00024 } 00025 00029 gaussian::gaussian(float mean, float var) { 00030 _mean1=mean; 00031 _var1=var; 00032 _std1=sqrtf(var); 00033 _ready=false; 00034 } 00035 00043 float gaussian::get_sample_1D() { 00044 //return a point drawn from a gaussian distribution centered at mean 00045 //with a given sigma^2 00046 00047 // polar form of a gaussian distribution from 00048 // http://www.taygeta.com/random/gaussian.html 00049 00050 00051 float x1, x2, w, y1; 00052 static float y2; 00053 00054 00055 if (_ready) { 00056 _ready=false; 00057 return y2*_std1+_mean1; 00058 } 00059 00060 _ready=true; 00061 00062 do { 00063 x1 = 2.0 * real_random() - 1.0; 00064 x2 = 2.0 * real_random() - 1.0; 00065 w = x1 * x1 + x2 * x2; 00066 } while (w>1.0 || w==0.0); 00067 00068 w = sqrtf((-2.0 * log(w))/w ); 00069 y1 = x1 * w; 00070 y2 = x2 * w; 00071 00072 float tmp=y1*_std1+_mean1; 00073 00074 return tmp; 00075 } 00076