/////////////////////////////////////////////////////////////////////// //hello.c #include "R.h" void hello(int* n){ int i; for(i = 0; i < *n; i++) Rprintf("Hi ya'll %d times!\n", i); *n += 1; } /////////////////////////////////////////////////////////////////////// //X.hh #ifndef X_HH #define X_HH class X{ int n; public: X(int N); ~X(); }; #endif //X.cc #include "X.hh" #include #include X::X(int N) { n = N; for(int i = 0; i < n; i++) Rprintf("Hello world %d times!\n", i); Rprintf("Normal %f \n", pnorm(1.96, 0, 1, 1, 0)); } X::~X() { for(int i = 0; i < n; i++) Rprintf("Goodbye world %d times!\n", i); } //useX.cc #include "X.hh" extern "C" { void func(int* n){ X x(*n); } } /////////////////////////////////////////////////////////////////////// //rngEx.cc #include #include extern "C" { void rngFunc(int* n, double* mu, double* sig, double* x) { GetRNGstate(); for(int i = 0; i < *n; i++) x[i] = rnorm(*mu, *sig); PutRNGstate(); } } /////////////////////////////////////////////////////////////////////// // FM.hh #ifndef FM_HH #define FM_HH class FM{ unsigned long a;//multiplier int expFor2;//exponent for 2 unsigned long power(int i, int k); public: FM();//constructor ~FM(){};//destructor void ranGen(unsigned long seed, int n, double* pu); }; #endif //FM.cc #include "FM.hh" unsigned long FM::power(int i, int k){ unsigned long iout = (unsigned long)i; if(k > 1) iout = iout*power(iout, k-1); return iout; } FM::FM(){ a=630360016; expFor2 = 31; } void FM::ranGen(unsigned long seed, int n, double* pu){ unsigned long m = power(2,expFor2)-1; for(int i = 0; i< n; i++){ seed = (a*seed)%m; pu[i] = ((double)seed)/((double)m); } } //fmR.cc #include "R.h" #include "FM.hh" extern "C" { void rngFM(int* N, int* SEED){ int n = *N; unsigned long seed = (unsigned long)(*SEED); FM* pFM = new FM(); double* pu = new double[n]; pFM->ranGen(seed, n, pu); Rprintf("Your random numbers are\n"); for(int i = 0; i < n; i++) Rprintf("%f \n", pu[i]); delete pFM; delete[] pu; } }