random.h

説明を見る。
00001 // -*-c++-*-
00002 
00008 /*
00009  *Copyright:
00010 
00011  Copyright (C) Hidehisa AKIYAMA
00012 
00013  This code is free software; you can redistribute it and/or
00014  modify it under the terms of the GNU Lesser General Public
00015  License as published by the Free Software Foundation; either
00016  version 2.1 of the License, or (at your option) any later version.
00017 
00018  This library is distributed in the hope that it will be useful,
00019  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  Lesser General Public License for more details.
00022 
00023  You should have received a copy of the GNU Lesser General Public
00024  License along with this library; if not, write to the Free Software
00025  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027  *EndCopyright:
00028  */
00029 
00031 
00032 #ifndef RCSC_UTIL_RANDOM_H
00033 #define RCSC_UTIL_RANDOM_H
00034 
00035 #include <boost/random.hpp>
00036 
00037 #include <algorithm> // min, max
00038 #include <iostream>
00039 #include <ctime>
00040 
00041 namespace rcsc {
00042 
00043 /*-------------------------------------------------------------------*/
00048 class RandomEngine {
00049 public:
00051     typedef boost::mt19937 base_type;
00052 private:
00054     base_type M_engine;
00055 
00062     RandomEngine()
00063         : M_engine( std::time( 0 ) )
00064       { }
00065 
00067     RandomEngine( const RandomEngine & );
00069     RandomEngine& operator=( const RandomEngine & );
00070 
00071 public:
00072 
00077     static
00078     RandomEngine & instance()
00079       {
00080           static RandomEngine s_instance;
00081           return s_instance;
00082       }
00083 
00088     void seed( base_type::result_type value )
00089       {
00090           M_engine.seed( value );
00091       }
00092 
00097     base_type & get()
00098       {
00099           return M_engine;
00100       }
00101 };
00102 
00103 /*-------------------------------------------------------------------*/
00109 template < typename DstType >
00110 class UniformRNG {
00111 public:
00113     typedef typename DstType::result_type result_type;
00115     typedef boost::variate_generator< RandomEngine::base_type&,
00116                                       DstType > Generator;
00117 private:
00119     Generator M_gen;
00120 
00122     UniformRNG();
00123 public:
00129     UniformRNG( result_type min,
00130                 result_type max )
00131         : M_gen( RandomEngine::instance().get(),
00132                  DstType( min, max ) )
00133       { }
00134 
00139     result_type operator()()
00140       {
00141           return M_gen();
00142       }
00143 };
00144 
00146 typedef UniformRNG< boost::uniform_smallint<> > UniformSmallInt;
00148 typedef UniformRNG< boost::uniform_int<> > UniformInt;
00150 typedef UniformRNG< boost::uniform_real<> > UniformReal;
00151 
00152 }
00153 
00154 #endif

librcscに対してThu May 1 15:41:21 2008に生成されました。  doxygen 1.5.0