00001 00002 /* 00003 The Extended Integer (XInt) Library 00004 A fast, portable C++ library for multi-precision integer math 00005 Copyright 2010 by Chad Nelson 00006 00007 Distributed under the Boost Software License, Version 1.0. 00008 See accompanying file LICENSE_1_0.txt or copy at 00009 http://www.boost.org/LICENSE_1_0.txt 00010 00011 See http://www.boost.org/libs/xint for library home page. 00012 */ 00013 00018 #ifndef BOOST_INCLUDED_XINT_RANDOM_HPP 00019 #define BOOST_INCLUDED_XINT_RANDOM_HPP 00020 00021 #include <ctime> 00022 #include <boost/random/uniform_int.hpp> 00023 #include <boost/random/variate_generator.hpp> 00024 #include <boost/random/mersenne_twister.hpp> 00025 00026 namespace boost { 00027 namespace xint { 00028 00029 namespace detail { 00031 00032 // This is a template only so that the static member variables don't cause 00033 // problems in header-only mode. 00034 template <typename T = void> 00035 class strong_random_generator_t { 00036 public: 00037 typedef unsigned int result_type; 00038 00039 strong_random_generator_t(); 00040 ~strong_random_generator_t(); 00041 result_type operator()(); 00042 00043 static const bool has_fixed_range; 00044 static const result_type min_value; 00045 static const result_type max_value; 00046 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const; 00047 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const; 00048 double entropy() const; 00049 00050 private: 00051 struct impl_t; 00052 impl_t *impl; 00053 }; 00054 00056 struct base_random_generator { 00057 typedef unsigned int result_type; 00058 virtual result_type operator()()=0; 00059 virtual ~base_random_generator() { } 00060 }; 00061 00063 template <class T> 00064 struct random_generator: public base_random_generator { 00065 typedef boost::uniform_int<result_type> dist_t; 00066 typedef boost::variate_generator<T&, dist_t> gen_t; 00067 random_generator(T& g): gen(g, 00068 dist_t((std::numeric_limits<result_type>::min)(), 00069 (std::numeric_limits<result_type>::max)())) { } 00070 result_type operator()() { return gen(); } 00071 gen_t gen; 00072 }; 00073 00075 } // namespace detail 00076 00086 class default_random_generator: public boost::mt19937 { 00087 public: 00088 default_random_generator(): mt19937(boost::uint32_t(time(0)+clock())) { } 00089 }; 00090 00097 typedef detail::strong_random_generator_t<> strong_random_generator; 00098 00099 } // namespace xint 00100 } // namespace boost 00101 00102 #endif // BOOST_INCLUDED_XINT_RANDOM_HPP
© Copyright Chad Nelson, 2010-2011. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)