00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanWhiteNoiseRandomGenerator.h" 00028 00029 #include "CeylanOperators.h" 00030 #include "CeylanLogPlug.h" 00031 #include "CeylanSystem.h" // for getPreciseTime 00032 00033 00034 #include <list> 00035 00036 #include <cstdlib> // for rand, srand 00037 00038 00039 00040 using std::string ; 00041 00042 00043 using namespace Ceylan ; 00044 using namespace Ceylan::System ; // for time primitives 00045 00046 using namespace Ceylan::Maths ; 00047 using namespace Ceylan::Maths::Random ; 00048 00049 using namespace Ceylan::Log ; 00050 00051 00052 00053 #ifdef CEYLAN_USES_CONFIG_H 00054 #include "CeylanConfig.h" // for CEYLAN_DEBUG_RANDOM and al 00055 #endif // CEYLAN_USES_CONFIG_H 00056 00057 00058 00059 00060 WhiteNoiseGenerator::WhiteNoiseGenerator( Sample lowerLimit, 00061 Sample upperLimit ) : 00062 RandomGenerator( lowerLimit, upperLimit ) 00063 { 00064 00065 #if CEYLAN_DEBUG_RANDOM 00066 LogPlug::trace( "Maths::WhiteNoiseGenerator constructor called." ) ; 00067 #endif // CEYLAN_DEBUG_RANDOM 00068 00069 generateSeedFromCurrentTime() ; 00070 preCompute() ; 00071 00072 #if CEYLAN_DEBUG_RANDOM 00073 LogPlug::trace( "Maths::WhiteNoiseGenerator constructor ended." ) ; 00074 #endif // CEYLAN_DEBUG_RANDOM 00075 00076 } 00077 00078 00079 00080 WhiteNoiseGenerator::WhiteNoiseGenerator( Sample lowerLimit, 00081 Sample upperLimit, Seed aSeed ) : 00082 RandomGenerator( lowerLimit, upperLimit, aSeed ) 00083 { 00084 00085 preCompute() ; 00086 00087 } 00088 00089 00090 00091 00092 WhiteNoiseGenerator::~WhiteNoiseGenerator() throw() 00093 { 00094 00095 #if CEYLAN_DEBUG_RANDOM 00096 LogPlug::trace( "Maths::WhiteNoiseGenerator destructor called." ) ; 00097 #endif // CEYLAN_DEBUG_RANDOM 00098 00099 } 00100 00101 00102 00103 void WhiteNoiseGenerator::generateSeedFromCurrentTime() 00104 { 00105 00106 try 00107 { 00108 00109 Second sec ; 00110 Microsecond microsec ; 00111 00112 getPreciseTime( sec, microsec ) ; 00113 _seed = static_cast<Seed>( sec + microsec ) ; 00114 00115 } 00116 catch( const SystemException & e ) 00117 { 00118 00119 throw MathsException( 00120 "WhiteNoiseGenerator::generateSeedFromCurrentTime: " 00121 "unable to forge random seed from current time: " 00122 + e.toString() ) ; 00123 00124 } 00125 00126 } 00127 00128 00129 00130 RandomValue WhiteNoiseGenerator::getNewValue() 00131 { 00132 00133 // rand returns int. 00134 00135 return static_cast<RandomValue>( _lowerLimit + 00136 static_cast<double>( ::rand() ) * 00137 ( _upperLimit - _lowerLimit )/ RAND_MAX ) ; 00138 00139 } 00140 00141 00142 00143 void WhiteNoiseGenerator::reset( Seed newSeed ) 00144 { 00145 00146 _seed = newSeed ; 00147 ::srand( _seed ) ; 00148 00149 } 00150 00151 00152 00153 const string WhiteNoiseGenerator::toString( VerbosityLevels level ) const 00154 { 00155 00156 return "White noise generator. " + RandomGenerator::toString( level ) ; 00157 00158 } 00159 00160 00161 00162 void WhiteNoiseGenerator::preCompute() 00163 { 00164 00165 // Initialize new sequence with seed. 00166 ::srand( _seed ) ; 00167 00168 } 00169