Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

random.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2004 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: random.h,v 1.4 2006/03/01 16:07:22 fruit Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #ifndef SALT_RANDOM_H
00023 #define SALT_RANDOM_H
00024 
00025 #include <boost/random.hpp>
00026 #include <boost/version.hpp>
00027 
00028 namespace salt
00029 {
00030 #if 0
00031 }
00032 #endif
00033 
00045 class RandomEngine : public boost::mt19937
00046 {
00047 public:
00048     typedef boost::mt19937::result_type result_type;
00049 
00050     static RandomEngine& instance()
00051     { static std::auto_ptr<RandomEngine> the_instance(new RandomEngine()); 
00052               return *the_instance; }
00053 
00054     static RandomEngine&
00055 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520)
00056     // Work around overload resolution problem (Gennadiy E. Rozental)
00057     instance(const result_type& value)
00058 #else
00059     instance(result_type value)
00060 #endif
00061     { instance().seed( value ); return instance(); }
00062 
00063     // For GCC, moving this function out-of-line prevents inlining, which may
00064     // reduce overall object code size.  However, MSVC does not grok
00065     // out-of-line definitions of member function templates.
00066     template<class Generator>
00067     static RandomEngine& instance(Generator& gen)
00068     { instance().seed( gen ); return instance(); }
00069 private:
00070     RandomEngine() : boost::mt19937() { }
00071 };
00072 
00073 #if (defined(BOOST_VERSION) && (BOOST_VERSION >= 103100))
00074 #include <boost/random/variate_generator.hpp>
00075 
00079 template<class RealType = double>
00080 class UniformRNG : public boost::variate_generator<salt::RandomEngine,
00081                                                    boost::uniform_real<RealType> >
00082 {
00083 public:
00084     UniformRNG(RealType min = RealType(0), RealType max = RealType(1))
00085         : boost::variate_generator<RandomEngine, boost::uniform_real<RealType> >
00086     (salt::RandomEngine::instance(), boost::uniform_real<RealType>(min,max))
00087     {}
00088 };
00089 
00092 template<class RealType = double>
00093 class NormalRNG : public boost::variate_generator<salt::RandomEngine,
00094                                                   boost::normal_distribution<RealType> >
00095 {
00096 public:
00097     NormalRNG(double mean, double sigma = (1))
00098         : boost::variate_generator<RandomEngine,
00099                                    boost::normal_distribution<RealType> >
00100     (salt::RandomEngine::instance(), boost::normal_distribution<RealType>(mean,sigma))
00101     {}
00102 };
00103 
00108 template<class RealType = double>
00109 class ExponentialRNG : public boost::variate_generator<salt::RandomEngine,
00110                                                        boost::exponential_distribution<RealType> >
00111 {
00112 public:
00113     ExponentialRNG(double lambda = RealType(1))
00114         : boost::variate_generator<RandomEngine,
00115                                    boost::exponential_distribution<RealType> >
00116     (salt::RandomEngine::instance(),
00117      boost::exponential_distribution<RealType>(lambda))
00118     {}
00119 };
00120 
00121 #else // BOOST_VERSION >= 103100
00122 
00126 template<class RealType = double>
00127 class UniformRNG : public boost::uniform_real<salt::RandomEngine,RealType>
00128 {
00129 public:
00130     UniformRNG(RealType min = RealType(0), RealType max = RealType(1))
00131         : boost::uniform_real<RandomEngine,RealType>
00132     (salt::RandomEngine::instance(),min,max)
00133     {}
00134 };
00135 
00138 template<class RealType = double>
00139 class NormalRNG : public boost::normal_distribution<salt::RandomEngine>
00140 {
00141 public:
00142     NormalRNG(double mean, double sigma = (1))
00143         : boost::normal_distribution<salt::RandomEngine>
00144     (salt::RandomEngine::instance(),mean,sigma)
00145     {}
00146 };
00147 
00152 template<class RealType = double>
00153 class ExponentialRNG : public boost::exponential_distribution<salt::RandomEngine>
00154 {
00155 public:
00156     ExponentialRNG(double lambda = RealType(1))
00157         : boost::exponential_distribution<salt::RandomEngine>
00158     (salt::RandomEngine::instance(),lambda)
00159     {}
00160 };
00161 
00162 #endif
00163 
00164 } // namespace salt
00165 
00166 #endif // SALT_RANDOM_H

Generated on Thu Apr 6 15:25:39 2006 for rcssserver3d by  doxygen 1.4.4