Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. double MAX = 20;
  8.  
  9. double f(double x)
  10. {
  11.     return x;
  12. }
  13.  
  14. double randomPoint(double a, double b) {
  15.     return a + (double)rand()/(double)(RAND_MAX+1) * (b-a);
  16. }
  17.  
  18. double integerMC(int a, int b, int P) {
  19.     int counter = 0;
  20.     double random_x;
  21.     double random_y;
  22.     for(int i = 0; i < P; i++)
  23.     {
  24.         random_x = randomPoint(a, b);
  25.         random_y = randomPoint(0, MAX);
  26.         if(random_y <= f(random_x)) counter++;
  27.     }
  28.     return counter/(double)P * (b-a) * MAX;
  29. }
  30.  
  31. int main() {
  32.     srand(time(NULL));
  33.     double pole = integerMC(0, MAX, 10000);
  34.     cout << pole;
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement