00001 // -*- c++ -*- 00002 // 00003 // $Id: rng.h 3343 2010-09-17 18:36:31Z java $ 00004 // 00005 // Copyright (C) 2009-2010 Rafael Ostertag 00006 // 00007 // This file is part of YAPET. 00008 // 00009 // YAPET is free software: you can redistribute it and/or modify it under the 00010 // terms of the GNU General Public License as published by the Free Software 00011 // Foundation, either version 3 of the License, or (at your option) any later 00012 // version. 00013 // 00014 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY 00015 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00016 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00017 // details. 00018 // 00019 // You should have received a copy of the GNU General Public License along with 00020 // YAPET. If not, see <http://www.gnu.org/licenses/>. 00021 // 00022 00023 #ifndef _RNG_H 00024 #define _RNG_H 00025 00026 #ifdef HAVE_CONFIG_H 00027 # include <config.h> 00028 #endif 00029 00030 #ifdef HAVE_STDEXCEPT 00031 # include <stdexcept> 00032 #endif 00033 00034 #ifdef HAVE_SYS_TYPES_H 00035 # include <sys/types.h> 00036 #endif 00037 00038 #include "../../intl.h" 00039 00040 #include "pwgenexception.h" 00041 00042 namespace YAPET { 00043 00044 namespace PWGEN { 00045 00046 enum RNGENGINE { 00047 DEVRANDOM = (1 << 0), 00048 DEVURANDOM = (1 << 1), 00049 LRAND48 = (1 << 2), 00050 RAND = (1 << 3), 00051 AUTO = (1 << 4), 00052 NONE = 0 00053 }; 00054 00071 class RNG { 00072 private: 00078 int fd; 00079 00086 bool rng_initialized; 00087 00093 RNGENGINE rng_used; 00094 00100 static int rng_available; 00101 00102 static void check_availability() throw (PWGenException); 00104 void init_rng (RNGENGINE request) throw (PWGenException); 00105 00106 00107 size_t devrandom (size_t ceil) throw (PWGenException); 00108 size_t _lrand48 (size_t ceil) throw(); 00109 size_t _rand (size_t ceil) throw(); 00110 00111 public: 00113 RNG (RNGENGINE request = AUTO) throw (PWGenException); 00114 RNG (const RNG& r) throw (PWGenException); 00115 virtual ~RNG() throw(); 00116 00117 size_t getRandomNumber (size_t ceil) throw (PWGenException); 00118 00119 static int getAvailableRNGs(); 00120 00121 RNGENGINE getRNGEngineUsed() const throw() { 00122 return rng_used; 00123 } 00124 00125 inline size_t operator() (size_t ceil) throw (PWGenException) { 00126 return getRandomNumber (ceil); 00127 } 00128 const RNG& operator= (const RNG& r) throw(); 00129 }; 00130 } 00131 } 00132 00133 #endif // _RNG_H
1.7.1