Also I was struck by how you were calling the function uniform_deviate
in a loop, but the way it is written it will always return the same
value, so what is the point of calling it repeatedly?
this is my current code on that section:
double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=(double)Vj/(N-1);
return U;
}
double normal_deviate ()
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 > 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}
important i guess to also note that in int main () Vj starts at a
value of 1.
i dont think that it calls the same number every time? i did a cout
<<U << in the uniform deviate function, and it was a different value
each time (because Vj keeps changing, its a very basic random number
generator, not a good one though). And the values in normal deviate
also appear to be changing, because i ran a cout <<Z << in there as
well, and it was numbers from the normal curve. Now each time i run
the program, it will give me the exact same "random" numbers i think,
but i think thats what we are supposed to do for this one. its a bad
random number generator mainly =P
What I am currently struggling with is the calculations within the int
main ().
I was miscalculating Am, because i was simply takign the last Vt value
and adding the last Si value from the loop.
here is the relevant code:
double Values[10];
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
Values
= exp(-r*T)*MAX(K-Si,0);
Vt = exp(-r*T)*MAX(K-Si,0);
//in this space here i need to calculate a sum of all the Si's. or
rather, need to use it in the line below. is there a sum function
where i could say sum Values[] and have it add all the numbers stored
in my array? because i need that sum divided by M, as seen below.