October 28th, 2008, 3:51 pm
Thanks a lot for your hints. But I haven't understood, why simple pointers likedouble* pointer = new double[N]//source codedelete pointer;can pose that many problems. Have I understood it correctly that using the STL vector class can help overcome those problems?And then I am experiencing another problem. I my MC simulation I am using the random number generator rand(). I am not initializing it via srand() in order to make sure that I always obtain the same sequence of random numbers for testing purposes. And hence I should always obtain the same option value. As a standard C++ console application everything works fine. However, after writing an Excel add-in, I always get different results depsite the fact of using the same set of parameters. My source code is as follows:double normal_dist(double expectation, double variance){ double help1 = (double)rand()/RAND_MAX; double help2 = (double)rand()/RAND_MAX; if (help1==0) help1+=0.00001; return expectation + sqrt(variance)*sqrt(-2*log(help1))*cos(2*PI*help2);}extern "C" _declspec(dllexport) double _stdcall BS_MC(double r, double X, double S0, double T, double sigma, int N){ //double* payoff = new double[N]; double payoff[M]; double value = 0; for(int i=0; i<M; i++){ payoff = exp(-r*T)*MAX(S0*exp((r-0.5*sigma*sigma)*T+normal_dist(0,sigma*sigma*T))-X,0.0); value+=payoff; } value/=M; //delete payoff; return value;}double MAX(double a, double b){ if(a>b) return a; else return b;}Does anyone see any major mistake I make? Or is there an explanation of whether or not calls from within Excel can generate different sequences of random numbers and if so, how this can be avoided? Thanks a lot in advance! At the moment all that seems really weird to me.