ownRandom.h
Go to the documentation of this file.
00001 
00006 #ifndef _OWN_RANDOM_H_
00007 #define _OWN_RANDOM_H_
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <math.h>
00012 #include <time.h>
00013 #include <string.h>
00014 #include <vector>
00015 
00021 class ownRandom{
00022                 public:
00023                                 float *normalRandomCache;
00024                                 bool isOk;
00025                                 int size;
00026                                 
00027                                 ownRandom(){
00028                                                 isOk=false;
00029                                                 size = 0;
00030                                                 allocate(10000);
00031                                                 srand( (unsigned)time( NULL ) ); 
00032                                 }
00033                                 ~ownRandom(){
00034                                 
00035                                 }
00037                                 void allocate(int m_size){
00038                                                 if(size != 0){ 
00039                                                                 if(normalRandomCache)free(normalRandomCache);
00040                                                 }
00041                                                  
00042                                                 normalRandomCache = (float *) malloc(sizeof(float)*m_size);
00043                                                 isOk=true;
00044                                                 size = m_size;
00045                                 }
00046                                 
00047                                 void fillCache(){
00048                                                 if(size > 0 && normalRandomCache != NULL){
00049                                                                 for(int i=0;i<size;i++){
00050                                                                                 normalRandomCache[i] = normalRandom(); 
00051                                                                                 isOk = true;     
00052                                                                 }
00053                                                 }else{
00054                                                                 allocate(10000);
00055                                                                 fillCache();
00056                                                 }
00057                                 }
00062                                 float getCachedUniformRandom(){
00063                                                 int rv;
00064                                                 float scale;
00065                                                 
00066                                                 if(!isOk) fillCache();
00067                                                 
00068                                                 scale = (float) size / (float)RAND_MAX;
00069                                                 rv = rand(); 
00070                                                 rv = (int) ((float) rv * scale);
00071                                                 if(rv >=size){
00072                                                                 fprintf(stderr,"ERROR in getCachedUniformRandom(). rv=%d\n",rv);
00073                                                                 rv = size-1; 
00074                                                 }
00075                                                 return normalRandomCache[rv];
00076                                 }
00077                                 
00081                                 float uniformRandom(void){
00082                                                 return ( ((float) rand() / (float) RAND_MAX));
00083                                 }
00084                                 
00089                                 float normalRandom()
00090                                 {
00091                                                 static float gset;
00092                                                 static int randomStored = 0;
00093                                                 float fac,rsq,v1,v2;
00094 
00095                                                 if(randomStored){
00096                                                                 return gset;
00097                                                 }
00098     
00099                                                 do {
00100                                                                 v1=2.0*uniformRandom()-1.0; //pick two uniform numbers in the square extending
00101                                                                                                                                                                                 //from -1 to +1 in each direction, 
00102                                                                 v2=2.0*uniformRandom()-1.0;
00103                                                                 rsq=v1*v1+v2*v2;                //see if they are in the unit circle,
00104                                                 } while (rsq >= 1.0 || rsq == 0.0); //if not try again
00105         
00106                                                 fac=sqrt(-2.0*log(rsq)/rsq);
00107                                                 gset=v1*fac; // can be used also as normal distributed random number!!
00108                                                 return v2*fac;
00109                                 }
00110                                 
00118                                 void covRandom(float& xRand, float& yRand, float cov[3]){
00119                                                 float a,b,c;
00120                                                 float eig1,eig2;
00121                                                 float vx1,vy1,vx2,vy2;
00122                                                 float d;
00123                                                 float x,y;
00124                                                 
00125                                                 xRand = normalRandom();
00126                                                 yRand = normalRandom();
00127                                                 // calculate eigen values
00128                                                 a = 1.0;
00129                                                 b = -(cov[0]+cov[2]);
00130                                                 c = cov[0]*cov[2] - cov[1]*cov[1];
00131 
00132                                                 eig1 = (-b + sqrt(b*b-4*a*c))/(2*a);
00133                                                 eig2 = (-b - sqrt(b*b-4*a*c))/(2*a);
00134 
00135                                                 // calculate eigen vectors
00136                                                 vx1 = cov[1];
00137                                                 vy1 = eig1 - cov[0];
00138                                                 d = sqrt(vx1*vx1+vy1+vy1);
00139                                                 vx1 /= d;
00140                                                 vy1 /= d;
00141 
00142                                                 vx2 = cov[1];
00143                                                 vy2 = eig2 - cov[0];
00144                                                 d = sqrt(vx2*vx2+vy2+vy2);
00145                                                 vx2 /= d;
00146                                                 vy2 /= d;
00147         
00148                                                 // Transform the random variable according to the covariance
00149                                                 x = xRand * sqrt(eig1);
00150                                                 y = yRand * sqrt(eig2);
00151         
00152                                                 xRand = x * vx1 + y * vx2;
00153                                                 yRand = x * vy1 + y * vy2;
00154                                 }
00155 };
00156 #endif
00157 


ndt_mcl
Author(s): jari
autogenerated on Mon Jan 6 2014 11:32:07