Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- double MAX = 20;
- double f(double x)
- {
- return x;
- }
- double randomPoint(double a, double b) {
- return a + (double)rand()/(double)(RAND_MAX+1) * (b-a);
- }
- double integerMC(int a, int b, int P) {
- int counter = 0;
- double random_x;
- double random_y;
- for(int i = 0; i < P; i++)
- {
- random_x = randomPoint(a, b);
- random_y = randomPoint(0, MAX);
- if(random_y <= f(random_x)) counter++;
- }
- return counter/(double)P * (b-a) * MAX;
- }
- int main() {
- srand(time(NULL));
- double pole = integerMC(0, MAX, 10000);
- cout << pole;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement