Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include "stdafx.h"
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- double Fun(double x) {
- return x*x;
- }
- double MonteCarlo(double a, double b) {
- srand(123975467);
- int countAll = 0;
- int countSquare = 0;
- double x;
- double y;
- double maxOfArea = (Fun(a) > Fun(b) ? Fun(a) : Fun(b));
- for (int i = 0; i < 1000000; i++) {
- x = ((rand() % 10000) / 10000.0) * (b - a) + a;
- y = ((rand() % 10000) / 10000.0) * maxOfArea;
- countAll += 1;
- // printf("%lf %lf\n", x, y);
- if (y <= Fun(x)) { //y не больше х*х в данной точке
- countSquare = countSquare + 1;
- // printf("aaa");
- }
- }
- printf("%d\n", countSquare);
- printf("%d\n", countAll);
- printf("Integral equals to ");
- double res = (b - a) * maxOfArea * countSquare / countAll;
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement