-2

I am writing a monte carlo program to do a 10D integral. But what I have started off with is trying to get the full method to work in a lower dimension before moving up to the higher level. Anyways I have this program and it is using the the struct random number generator given in Numerical Recipes. Now when I run a program and just outputs the the random number it works fine, and the same when I use the built in rand function for the actual integration as well. The problem is that when I combine the two I get that the way I call the RNG struct it says that I am using 'of undeclared identifier' for both instances that I am trying to call this struct. I honestly have no idea why its throwing this error and how to fix it. Any suggestions would be greatly appreciated. The code that I am referencing is here and the error is stated as

Montetest.cpp:50:17: error: use of undeclared identifier 'rndvar' double u1 = rndvar.int64(); Montetest.cpp:51:17: error: use of undeclared identifier 'rndvar' double u2 = rndvar.int64();"

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;


struct Ran {
/* 
 Implementation of the highest quality recommended generator. The constructor 
is called with
 an integer seed and creates an instance of the generator. The member 
functions int64, doub, 
 and int32 return the next values in the random sequence, as a variable type 
indicated by 
 their names. The period of the generator is roughly 3.138 x 10^57.
*/
   unsigned long u,v,w;
   Ran(unsigned long j) : v(4101842887655102017LL), w(1) {
// Constructor. Call with any integer seed (except value of v above).
      u = j ^ v; int64();
      v = u; int64();
      w = v; int64();
   }
   inline unsigned long int64() {
// Return 64-bit random integer.
      u = u * 2862933555777941757LL + 7046029254386353087LL; 
      v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
      w = 4294957665U*(w & 0xffffffff) + (w >> 32);
      unsigned long x = u ^ (u << 21); x ^= x >> 35; x ^= x << 4; 
      return (x + v) ^ w;
   }
   inline double doub() { return 5.42101086242752217E-20 * int64(); }
 // Return random double-precision floating value in the range 0. to 1.
   inline unsigned int int32() { return (unsigned int)int64(); }
// Return 32-bit random integer.
};


//int count;
double total, inBox;

// user defined function below
double f (double x){
  return exp(cos(x));
}
//

//function to calculate a definite integral given bounds of integration 
(xmin/max) & bounds of function (ymin/ymax)
double integral (double (*f)(double), double xmin, double xmax, double ymin, 
double ymax,int n){
  for (int count=0; count < n; count++){
    double u1 = rndvar.int64();
    double u2 = rndvar.int64();

    double xcoord = ((xmax - xmin)*u1) + xmin;
    double ycoord = ((ymax - ymin)*u2) + ymin;
    double val = f(xcoord);

     total++;

    if (val > ycoord){
      inBox++;
    }
  }

  double density = inBox/total;

  std::cout<<(xmax - xmin)*(ymax - ymin)*density<<std::endl;
}


int main (int argc, char **argv)
{

   if(argc != 3) {
      printf("Need 2  arguments: seed  n\n");
      exit(0);
   }

   unsigned long iseed=atol(argv[1]);
   int n=atoi(argv[2]);   // number of steps
   vector <int> test;

   Ran rndvar(iseed);

   cout<< "RESULT: " <<endl;
   integral(f,-2,2,0,4,n);
}
Robert
  • 153
  • 7
  • `rndvar` is declared in `main` and you are trying to use it in a different function - the error is pretty self-explanatory – UnholySheep Mar 08 '18 at 18:15

2 Answers2

2

rndvar is declared in main but used in integral function.

Pass rndvar to integral function, or declare it in integral function, or make it a global variable (generally not recommended).

In C++11 you do not need to implement your own pseudo-random number generators. You can use the standard ones, see the example in pseudo-random number generation.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
0

Correct me if I am wrong but I think the problem is that your rndvar is declared and initialized outside of your integral function. This means that integral() has no knowledge of rndvar because it is outside of integral()'s scope.

Try passing in rndvar as a parameter to integral() I believe that will fix your issue.

TeeseCaprice
  • 55
  • 1
  • 13