Sacado_RandomImp.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #include <cmath>
00033 #include <cstdlib>
00034 #include <iostream>
00035 #include <cstdlib>
00036 
00037 template <typename ScalarT>
00038 Sacado::Random<ScalarT>::
00039 Random() :
00040   a(0.0),
00041   b(1.0),
00042   seed(static_cast<ScalarT>(rand()))
00043 {
00044   // rand() can return 0 or 2147483647, so adjust seed if that happens
00045   if ((seed == 0.0) || (seed == 2147483647.0))
00046     seed = 1.0;
00047 }
00048 
00049 template <typename ScalarT>
00050 Sacado::Random<ScalarT>::
00051 Random(ScalarT a_, ScalarT b_) :
00052   a(a_),
00053   b(b_),
00054   seed(static_cast<ScalarT>(rand()))
00055 {
00056   // rand() can return 0 or 2147483647, so adjust seed if that happens
00057   if ((seed == 0.0) || (seed == 2147483647.0))
00058     seed = 1.0;
00059 }
00060 
00061 template <typename ScalarT>
00062 Sacado::Random<ScalarT>::
00063 Random(ScalarT a_, ScalarT b_, int s) :
00064   a(a_),
00065   b(b_),
00066   seed(0.0)
00067 {
00068   setSeed(s);
00069 }
00070 
00071 template <typename ScalarT>
00072 Sacado::Random<ScalarT>::
00073 ~Random()
00074 {
00075 }
00076 
00077 template <typename ScalarT>
00078 void
00079 Sacado::Random<ScalarT>::
00080 setSeed(int s) {
00081   int ss = checkSeed("setSeed", s);
00082   srand(ss);
00083   seed = static_cast<ScalarT>(s);
00084 }
00085 
00086 template <typename ScalarT>
00087 ScalarT
00088 Sacado::Random<ScalarT>::
00089 number() {
00090   const ScalarT A = 16807.0;
00091   const ScalarT bigInt = 2147483647.0;
00092       
00093   seed = std::fmod(A*seed, bigInt);
00094   return (b-a)*(seed/bigInt) + a;
00095 }
00096 
00097 template <typename ScalarT>
00098 int
00099 Sacado::Random<ScalarT>::
00100 checkSeed(const std::string& func, int s) {
00101   if ((s < 1) || (s > 2147483646)) {
00102     std::cerr << "Error in Sacado::Random::" << s << "():  " 
00103         << "supplied seed " 
00104         << s << " is not an integer between 1 and 2147483646." 
00105         << std::endl << "Using a seed of 1 instead." << std::endl;
00106     return 1;
00107   }
00108   else
00109     return s;
00110 }
00111 
00112 #ifdef HAVE_SACADO_COMPLEX
00113 
00114 template <typename T>
00115 Sacado::Random< std::complex<T> >::
00116 Random() :
00117   rand_real(0.0, 1.0),
00118   rand_imag(0.0, 1.0)
00119 {
00120 }
00121 
00122 template <typename T>
00123 Sacado::Random< std::complex<T> >::
00124 Random(const std::complex<T>& a, const std::complex<T>& b) :
00125   rand_real(a.real(), b.real()),
00126   rand_imag(a.imag(), b.imag())
00127 {
00128 }
00129 
00130 template <typename T>
00131 Sacado::Random< std::complex<T> >::
00132 Random(const std::complex<T>& a, const std::complex<T>& b, int s) :
00133   rand_real(a.real(), b.real(), s),
00134   rand_imag(a.imag(), b.imag(), s+1)
00135 {
00136 }
00137 
00138 template <typename T>
00139 Sacado::Random< std::complex<T> >::
00140 ~Random()
00141 {
00142 }
00143 
00144 template <typename T>
00145 void
00146 Sacado::Random< std::complex<T> >::
00147 setSeed(int s) {
00148   rand_real.setSeed(s);
00149   rand_imag.setSeed(s+1);
00150 }
00151 
00152 template <typename T>
00153 std::complex<T>
00154 Sacado::Random< std::complex<T> >::
00155 number() {
00156   return std::complex<T>(rand_real.number(), rand_imag.number());
00157 }
00158 
00159 #endif // HAVE_SACADO_COMPLEX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:34 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3