Random.h
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2009 Savarese Software Research Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * https://www.savarese.com/software/ApacheLicense-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 #ifndef __SSRC_WSPR_UTILITY_RANDOM_H 00023 #define __SSRC_WSPR_UTILITY_RANDOM_H 00024 00025 #include <ssrc/wispers-packages.h> 00026 00027 #include <fcntl.h> 00028 #include <unistd.h> 00029 00030 #include <cerrno> 00031 #include <cstring> 00032 #include <stdexcept> 00033 00034 #include <boost/integer_traits.hpp> 00035 #include <boost/nondet_random.hpp> 00036 #include <boost/random/uniform_int.hpp> 00037 #include <boost/random/variate_generator.hpp> 00038 00039 __BEGIN_NS_SSRC_WSPR_UTILITY 00040 00041 class random_device { 00042 int _fd; 00043 00044 public: 00045 explicit random_device(const std::string & device_file = "/dev/urandom") 00046 SSRC_DECL_THROW(std::runtime_error) : 00047 _fd(::open(device_file.c_str(), O_RDONLY)) 00048 { 00049 if(_fd < 0) 00050 throw std::runtime_error(std::strerror(errno)); 00051 } 00052 00053 ~random_device() { 00054 ::close(_fd); 00055 } 00056 00057 template<typename result_type> 00058 void read(result_type & result) const SSRC_DECL_THROW(std::runtime_error) { 00059 if(::read(_fd, &result, sizeof(result_type)) != sizeof(result_type)) 00060 throw std::runtime_error(std::strerror(errno)); 00061 } 00062 }; 00063 00064 template<typename int_type> 00065 class random_engine { 00066 random_device _dev_random; 00067 00068 public: 00069 typedef int_type result_type; 00070 static const bool has_fixed_range = true; 00071 static const result_type min_value = 00072 boost::integer_traits<result_type>::const_min; 00073 static const result_type max_value = 00074 boost::integer_traits<result_type>::const_max; 00075 00076 explicit random_engine(const std::string & device_file = "/dev/urandom") 00077 SSRC_DECL_THROW(std::runtime_error) : 00078 _dev_random(device_file) 00079 { } 00080 00081 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { 00082 return min_value; 00083 } 00084 00085 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { 00086 return max_value; 00087 } 00088 00089 double entropy() const { 00090 return 10; 00091 } 00092 00093 result_type operator()() const SSRC_DECL_THROW(std::runtime_error) { 00094 result_type result; 00095 _dev_random.read(result); 00096 return result; 00097 } 00098 }; 00099 00100 template<typename int_type, 00101 template <typename U> class distribution = boost::uniform_int, 00102 class engine = random_engine<unsigned int> > 00103 class Random { 00104 public: 00105 typedef int_type result_type; 00106 typedef boost::integer_traits<result_type> result_type_traits; 00107 typedef engine random_engine; 00108 typedef distribution<result_type> random_distribution; 00109 00110 private: 00111 random_engine _engine; 00112 boost::variate_generator<random_engine &, random_distribution> _random; 00113 00114 public: 00115 00116 explicit Random(result_type min = result_type_traits::const_min, 00117 result_type max = result_type_traits::const_max) : 00118 _engine(), _random(_engine, random_distribution(min, max)) 00119 { } 00120 00121 result_type operator()() { 00122 return _random(); 00123 } 00124 }; 00125 00126 00127 __END_NS_SSRC_WSPR_UTILITY 00128 00129 #endif
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.