Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <vector>
  6. using namespace std;
  7.  
  8.  
  9. struct Ran {
  10. /*
  11.  Implementation of the highest quality recommended generator. The constructor is called with
  12.  an integer seed and creates an instance of the generator. The member functions int64, doub,
  13.  and int32 return the next values in the random sequence, as a variable type indicated by
  14.  their names. The period of the generator is roughly 3.138 x 10^57.
  15. */
  16.    unsigned long u,v,w;
  17.    Ran(unsigned long j) : v(4101842887655102017LL), w(1) {
  18. // Constructor. Call with any integer seed (except value of v above).
  19.       u = j ^ v; int64();
  20.       v = u; int64();
  21.       w = v; int64();
  22.    }
  23.    inline unsigned long int64() {
  24. // Return 64-bit random integer.
  25.       u = u * 2862933555777941757LL + 7046029254386353087LL;
  26.       v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
  27.       w = 4294957665U*(w & 0xffffffff) + (w >> 32);
  28.       unsigned long x = u ^ (u << 21); x ^= x >> 35; x ^= x << 4;
  29.       return (x + v) ^ w;
  30.    }
  31.    inline double doub() { return 5.42101086242752217E-20 * int64(); }
  32. // Return random double-precision floating value in the range 0. to 1.
  33.    inline unsigned int int32() { return (unsigned int)int64(); }
  34. // Return 32-bit random integer.
  35. };
  36.  
  37.  
  38. //int count;
  39. double total, inBox;
  40.  
  41. // user defined function below
  42. double f (double x){
  43.   return exp(cos(x));
  44. }
  45. //
  46.  
  47. //function to calculate a definite integral given bounds of integration (xmin/max) & bounds of function (ymin/ymax)
  48. double integral (double (*f)(double), double xmin, double xmax, double ymin, double ymax,int n){
  49.   for (int count=0; count < n; count++){
  50.     double u1 = rndvar.int64();
  51.     double u2 = rndvar.int64();
  52.  
  53.     double xcoord = ((xmax - xmin)*u1) + xmin;
  54.     double ycoord = ((ymax - ymin)*u2) + ymin;
  55.     double val = f(xcoord);
  56.  
  57.     total++;
  58.  
  59.     if (val > ycoord){
  60.       inBox++;
  61.     }
  62.   }
  63.  
  64.   double density = inBox/total;
  65.  
  66.   std::cout<<(xmax - xmin)*(ymax - ymin)*density<<std::endl;
  67. }
  68.  
  69.  
  70. int main (int argc, char **argv)
  71. {
  72.  
  73.    if(argc != 3) {
  74.       printf("Need 2  arguments: seed  n\n");
  75.       exit(0);
  76.    }
  77.  
  78.    unsigned long iseed=atol(argv[1]);
  79.    int n=atoi(argv[2]);   // number of steps
  80.    vector <int> test;
  81.  
  82.    Ran rndvar(iseed);
  83.  
  84.    cout<< "RESULT: " <<endl;
  85.    integral(f,-2,2,0,4,n);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement