Theo Vaf

Theo Vaf

  • NA
  • 1
  • 0

how to convert a c++ class into a C# one

Jun 11 2006 4:52 AM

Well here is my newb problem:
I have a random generator class written in c++ (numerical recipes).
In c# i add a class item RG.cs and then i am confused.Do i copy and paste everything from the .h file and the .cpp file of my c++ class?When I do that I get a lot of errors.What transformation do i have to do? 
Another question is how can i declare in c# a method that in c++ would be double ran2(long&);?

Here is the .h file:
//------------------------
class Rand
{
   public:
   // default constructor generates 1st random number and stores it in r
   Rand(long);
   // returns a uniform Random deviate between 0.0 and 1.0
   double ran2(long&);
   // Generates a new random number, stores it in r and then returns r
   double NewRand();
   // returns a random number
   double GetRand();
   private:
   // seed
   long num;
   // random number
   double r;
} ;
//----------------------
here is the .cpp file

Rand::Rand(
long dummy)
{
   num = -dummy;
  
// Initialize random number generator
  
ran2(num);
  
// stores random number in r
  
r = ran2(num);
}

// returns random number
double Rand::GetRand()
{
  
return r;
}

// generates new random number, stores in r and then returns r
double Rand::NewRand()
{
   r = ran2(num);
  
return r;
}

double Rand::ran2(long& idum)
{
  
const long IM1=2147483563;
  
const long IM2=2147483399;
  
const double AM=(1.0/IM1);
  
const long IMM1=(IM1-1);
  
const long IA1=40014;
  
const long IA2=40692;
  
const long IQ1=53668;
  
const long IQ2=52774;
  
const long IR1=12211;
  
const long IR2=3791;
  
const long NTAB=32;
  
const long NDIV=(1+IMM1/NTAB);
  
const double EPS=1.2e-7;
  
const double RNMX=(1.0-EPS);
  
int j;
  
long k;
  
static long idum2=123456789;
  
static long iy=0;
  
static long iv[NTAB];
  
double temp;
  
if(idum <= 0)
      {
        
if(-(idum) < 1)
             idum = 1;
         
else
            
idum = -(idum);
        
         idum2 = (idum);
        
for(j=NTAB+7;j >= 0;j--)
            {
                k=(idum)/IQ1;
                idum=IA1*(idum-k*IQ1)-k*IR1;
               
                if(idum < 0)
                    idum+=IM1;
               
if(j < NTAB)
                    iv[j] =idum;
           }
        iy = iv[0];
      }

   k = idum/IQ1;
   idum = IA1*(idum - k*IQ1) - k*IR1;

   if
(idum < 0)
      idum += IM1;

   k = idum2/IQ2;
   idum2 = IA2*(idum2-k*IQ2)-k*IR2;

   if(idum2 < 0)
      idum2 += IM2;

   j=iy/NDIV;
   iy=iv[j]-idum2;
   iv[j] = idum;

   if (iy < 1)
      iy += IMM1;

   if ((temp = AM*iy) > RNMX)
      return RNMX;
   else
     
return temp;
}


//------
Thanks all!