/*****************************************************************************
 This code is available for academic use
 under the LESSER GENERAL PUBLIC LICENSE 

 Weight Perturbation: enhancing local search strategies
 by perturbing the weights of training instances.
 Copyright (C) 2002  Gal Elidan, Matan Ninio, Nir Friedman and Dale Schuurmans

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 Please cite using this refrence:
 
@incollection{Elidan+al:2002,
   author = "Gal Elidan and Matan Ninio and Nir Friedman and Dale Schuurmans",
   booktitle = "Proc. National Conference on Artificial Intelligence (AAAI-02)",
   pages = "132-139",
   year = "2002",
   title = "Data Perturbation for Escaping Local Maxima in Learning",
 }
 
 You can contact the authors at annealing@cs.huji.ac.il
 
*****************************************************************************/

#ifndef RandomProb_h
#define RandomProb_h

/** Marsaglia's subtractive R.N. generator with carry; combined with a weyl
    generator. 
    Source: Computer Physics Communications 60 (1990) 345-349.
    Written by Geoffrey Zweig, 1992.
*/
class tMarsagliaGenerator
{
public:
  ///
  tMarsagliaGenerator(unsigned query=0);
  ///
  void Initialize(unsigned start);
  ///
  unsigned long RandomLong() {return Next();} 

  ///
  unsigned long RandomLong(unsigned long range)
  {return (Next() % range);}

  ///
  unsigned RandomInt(unsigned range) {return unsigned(Next() % range);} 

  ///
  double RandomDouble(double range);	

private:
  ///
  unsigned long word1[44];
  ///
  unsigned long weyl;
  ///
  int i,j,carry;
  ///
  unsigned long Next();
}///
;

///
class tRandomGenerator : public tMarsagliaGenerator {
public:
  ///
  double RandomProb();

  ///
  double DblGammaGreaterThanOne(double dblAlpha);
  ///
  double DblGammaLessThanOne(double dblAlpha);
  ///
  double DblRanGamma(double dblAlpha);
private:
}///
;

///
extern tRandomGenerator _RandomProbGenerator;

///
inline
double RandomProb(void)
{
  return _RandomProbGenerator.RandomProb();
}

#endif

