Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
1 --calc_func : declared in main module as function
2 --maxN : amount of randoms to proceed
3 --rv : round to value actualy may be nill too
4 function MonteCarlo_Method(calc_func,maxN,rv)
5 print("\nMonte Carlo method");
6 print("I=(b-a)*Sigma[f(xi)]/N");
7 print("Requested "..maxN.." random values");
8 local curentI=0;
9 local tv;
10 local brange=maxN;--just store in local
11 local bord1=startval*brange;--low level
12 local bord2=(endval*brange)+1;--hight level
13 --begin sumarize function value in random x place
14 for ti=1,brange do
15 --get random value between a and b
16 tv=(random(bord1,bord2))/brange;
17 curentI=curentI+calc_func(tv);
18 end --of for
19 --do it by formula
20 curentI=roundval((curentI*(endval-startval))/(brange),rv);
21 return curentI;
22 end