ppALIGN API documentation
src/drand48.hpp
00001 /****************************************************************************** 00002 Copyright 2009 Stefan Wolfsheimer & Gregory Nuel. 00003 00004 This file is part of ppALIGN 00005 00006 ppALIGN is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 ppALIGN is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with ppALIGN; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 *******************************************************************************/ 00020 /* This is a wrapper class to the build in 00021 rand48 RNG 00022 author: stefan wolfsheimer 2009 00023 */ 00024 00025 #ifndef _DRAND48_HPP_ 00026 #define _DRAND48_HPP_ 00027 00028 #define RAND48_SEED_0 (0x330e) 00029 #include <stdlib.h> 00030 #include <iostream> 00031 #include <string> 00032 #include <iomanip> 00033 #include <sstream> 00034 00038 class DRand48 00039 { 00040 private: 00041 unsigned short __rand48_seed[3]; 00042 public: 00047 DRand48(long _seed) 00048 { 00049 __rand48_seed[0] = RAND48_SEED_0; 00050 __rand48_seed[1] = (unsigned short) _seed; 00051 __rand48_seed[2] = (unsigned short) (_seed >> 16); 00052 } 00053 00054 DRand48() 00055 { 00056 long seed = 1; 00057 __rand48_seed[0] = RAND48_SEED_0; 00058 __rand48_seed[1] = (unsigned short) seed; 00059 __rand48_seed[2] = (unsigned short) (seed >> 16); 00060 } 00061 00063 DRand48(const DRand48 & rand) 00064 { 00065 __rand48_seed[0] = rand.__rand48_seed[0]; 00066 __rand48_seed[1] = rand.__rand48_seed[1]; 00067 __rand48_seed[2] = rand.__rand48_seed[2]; 00068 } 00069 00070 00072 inline double operator()(); 00073 00074 00076 inline void seed(long seed); 00077 00078 00085 inline void seed(const std::string & seed_str); 00086 00087 00088 00089 inline DRand48 & operator=(const DRand48 & rand); 00090 00092 friend inline std::ostream & operator<<(std::ostream & ost, const DRand48 & r); 00093 00095 friend inline std::istream & operator>>(std::istream & ost, DRand48 & r); 00096 }; 00097 00099 //void DRand48::seed(const std::string & seed_str) 00100 00101 00102 double DRand48::operator()() 00103 { 00104 return erand48(__rand48_seed); 00105 } 00106 00107 DRand48 & DRand48::operator=(const DRand48 & rand) 00108 { 00109 __rand48_seed[0] = rand.__rand48_seed[0]; 00110 __rand48_seed[1] = rand.__rand48_seed[1]; 00111 __rand48_seed[2] = rand.__rand48_seed[2]; 00112 return *this; 00113 } 00114 00115 inline void DRand48::seed(long seed) 00116 { 00117 __rand48_seed[0] = RAND48_SEED_0; 00118 __rand48_seed[1] = (unsigned short) seed; 00119 __rand48_seed[2] = (unsigned short) (seed >> 16); 00120 } 00121 00122 inline void DRand48::seed(const std::string & seed_str) 00123 { 00124 if(seed_str.length() == 4+4+4+2) 00125 { 00126 if(seed_str[4] == '.' && seed_str[9] == '.') 00127 { 00128 std::stringstream tmp(seed_str); 00129 tmp >> *this; 00130 } 00131 } 00132 else 00133 { 00134 long _seed = atol(seed_str.c_str()); 00135 seed(_seed); 00136 } 00137 } 00138 00139 00140 std::ostream & operator<<(std::ostream & ost, const DRand48 & r) 00141 { 00142 std::ios_base::fmtflags f = ost.flags(); 00143 ost.flags(std::ios::right | std::ios::hex) ; 00144 ost << std::setw(4) << std::setfill('0') 00145 << r.__rand48_seed[0] << "." 00146 << std::setw(4) << std::setfill('0') 00147 << r.__rand48_seed[1] << "." 00148 << std::setw(4) << std::setfill('0') 00149 << r.__rand48_seed[2]; 00150 ost.flags(f); 00151 return ost; 00152 } 00153 00154 std::istream & operator>>(std::istream & ist, DRand48 & r) 00155 { 00156 std::ios_base::fmtflags f = ist.flags(); 00157 char c; 00158 ist.flags(std::ios::hex) ; 00159 ist >> r.__rand48_seed[0] >> c 00160 >> r.__rand48_seed[1] >> c 00161 >> r.__rand48_seed[2]; 00162 00163 ist.flags(f); 00164 return ist; 00165 00166 } 00167 00168 00169 00170 00171 #endif