Back to index

CRandom.C

 
//----------------------------------------------------------------------------- 
//  $Header: /tmp_mnt/asis/offline/ceres/cool/project/RCS/CRandom.C,v 2.1 1996/10/04 08:45:39 voigt Exp $ 
// 
//  COOL Program Library   
//  Copyright (C) CERES collaboration, 1996 
// 
//  Implementation of class CRandom. 
// 
//----------------------------------------------------------------------------- 
#include "CRandom.h"  
 
CRandom::CRandom()  
{ 
   nextGaussPresent = False;  
} 
 
CRandom::CRandom(long seed) 
{ 
   nextGaussPresent = False; 
   setSeed(seed);  
} 
 
CRandom::~CRandom() {} 
 
double CRandom::gauss()  
{ 
   if (nextGaussPresent) { 
      nextGaussPresent = False; 
      return nextGauss; 
   } 
    
   double v1, v2, r, fac; 
    
   do { 
      v1 = 2.0 * flat() - 1.0; 
      v2 = 2.0 * flat() - 1.0; 
      r = v1*v1 + v2*v2; 
   } while ( r > 1.0 ); 
    
   fac = sqrt(-2.0*log(r)/r); 
   nextGauss = v1*fac; 
   nextGaussPresent = True; 
   return v2*fac; 
} 
 
long CRandom::poisson(double mean) 
{ 
   double a = 1; 
   double p = exp(-mean); 
    
   for (int k=0; ; k++) { 
      a *= flat(); 
      if (a < p) return k; 
   } 
} 

Back to index