PhotosRandom.cxx
00001 #include <iostream>
00002 #include "PhotosRandom.h"
00003 #include "Photos.h"
00004 #include "Log.h"
00005
00006 namespace Photospp
00007 {
00008
00009 bool PhotosRandom::init = false;
00010 int PhotosRandom::iseed[2]= { 1802, 9373 };
00011 int PhotosRandom::i97 = 96;
00012 int PhotosRandom::j97 = 32;
00013 double PhotosRandom::uran[97]= { 0.0 };
00014 double PhotosRandom::cran = 362436.0 /16777216.0;
00015 const double PhotosRandom::cdran = 7654321.0 /16777216.0;
00016 const double PhotosRandom::cmran = 16777213.0/16777216.0;
00017
00018
00019
00020 extern "C" double phoranc_(int *idum)
00021 {
00022 return Photos::randomDouble();
00023 }
00024
00025 void PhotosRandom::setSeed(int s1,int s2)
00026 {
00027 if(s1<0 || s1>31327) Log::Fatal("PhotosRandom::setSeed(): Seed(1) out of range [0,31327]",8);
00028 if(s2<0 || s2>30080) Log::Fatal("PhotosRandom::setSeed(): Seed(2) out of range [0,30080]",9);
00029 iseed[0]=s1;
00030 iseed[1]=s2;
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 void PhotosRandom::initialize()
00046 {
00047 long IS1,IS2,IS3,IS4,IS5;
00048 double S,T;
00049
00050
00051 IS1=(iseed[0]/177)%177+2;
00052 IS2= iseed[0]%177+2;
00053 IS3=(iseed[1]/169)%178+1;
00054 IS4= iseed[1]%169;
00055 for(int i=0;i<97;i++)
00056 {
00057 S=0.0;
00058 T=0.5;
00059 for(int j=0;j<24;j++)
00060 {
00061 IS5=( ((IS1*IS2)%179)*IS3 )%179;
00062 IS1=IS2;
00063 IS2=IS3;
00064 IS3=IS5;
00065 IS4=(53*IS4+1)%169;
00066 if( (IS4*IS5)%64>=32) S=S+T;
00067 T=0.5*T;
00068 }
00069 uran[i]=S;
00070 }
00071 init=true;
00072 Log::Debug(0)<<"PhotosRandom::inititalize(): seed: "<<iseed[0]<<", "<<iseed[1]<<std::endl;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 double PhotosRandom::randomReal()
00092 {
00093 if(!init) Log::Fatal("PhotosRandom::randomReal(): generator not initialized",1);
00094 double ret=0.0;
00095 while(true)
00096 {
00097 ret = uran[i97]-uran[j97];
00098 if(ret<0.0) ret+=1.;
00099 uran[i97]=ret;
00100 i97--;
00101 if(i97<0) i97=96;
00102 j97--;
00103 if(j97<0) j97=96;
00104 cran-=cdran;
00105 if(cran<0.0) cran+=cmran;
00106 ret-=cran;
00107 if(ret<0.0) ret+=1.0;
00108 if(ret>0.0) break;
00109 }
00110 return ret;
00111 }
00112
00113 }
00114