/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
 *
 *  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
 *
 *  Contact:
 *  Laboratoire de Vision et Systemes Numeriques
 *  Departement de genie electrique et de genie informatique
 *  Universite Laval, Quebec, Canada, G1K 7P4
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/Randomizer.hpp
 *  \brief  Definition of the class Randomizer.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.3 $
 *  $Date: 2004/04/25 23:12:21 $
 */

#ifndef Beagle_Randomizer_hpp
#define Beagle_Randomizer_hpp

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/AllocatorT.hpp"
#include "beagle/PointerT.hpp"
#include "beagle/ContainerT.hpp"
#include "beagle/WrapperT.hpp"
#include "beagle/Long.hpp"
#include "beagle/ULong.hpp"


namespace Beagle {

// Forward declaration
class System;

/*!
 *  \class Randomizer beagle/Randomizer.hpp "beagle/Randomizer.hpp"
 *  \brief Abstract interface of the random number generator.
 *  \ingroup ECF
 *  \ingroup Sys
 *  \par Note:
 *    The operator() allow compliance with the STL random number generator interface.
 */
class Randomizer : public Object {

public:

  //! Randomizer allocator type.
  typedef AllocatorT<Randomizer,Object::Alloc>
          Alloc;
  //! Randomizer handle type.
  typedef PointerT<Randomizer,Object::Handle>
          Handle;
  //! Randomizer bag type.
  typedef ContainerT<Randomizer,Object::Bag>
          Bag;

           Randomizer();
  virtual ~Randomizer() { }

  /*!
   *  \brief Return an randomly generated integer in the range [0,inN).
   */
  inline unsigned long operator()(unsigned long inN)
  {
    return rollInteger(0, inN-1);
  }

  virtual void   initialize(System& ioSystem);
  virtual void   postInit(System& ioSystem);
          double rollGaussian(double inMu=0.0, double inSigma=1.0);
  
  /*!
   *  \brief Initialize the randomizer state.
   *  \param inState Value of the state. If 0, set to seed value (if seed 0, set to timer value).
   */
  virtual void initRandomizer(unsigned long inState) =0;

  /*!
   *  \brief Generate a integer following an uniform discrete distribution.
   *  \param inLower Lower bound of the distribution.
   *  \param inUpper Upper bound of the distribution.
   *  \return Random number generated.
   */
  virtual unsigned long rollInteger(unsigned long inLower=0, unsigned long inUpper=ULONG_MAX) =0;
  
  /*!
   *  \brief Generate a number following a uniform distribution in the interval [inLower,inUpper).
   *  \param inLower Lower bound of the uniform distribution.
   *  \param inUpper Upper bound of the uniform distribution.
   *  \return Random number generated.
   */
  virtual double rollUniform(double inLower=0.0, double inUpper=1.0) =0;

protected:
  ULong::Handle mSeed;   //!< Seed of the random number generator.
  ULong::Handle mState;  //!< Value of the state of the random number generator.

};

}

#endif // Beagle_Randomizer_hpp
