00001 // $Id: NOX_Random.C,v 1.7 2006/08/22 00:01:27 rppawlo Exp $ 00002 // $Source: /space/CVS/Trilinos/packages/nox/src/NOX_Random.C,v $ 00003 00004 //@HEADER 00005 // ************************************************************************ 00006 // 00007 // NOX: An Object-Oriented Nonlinear Solver Package 00008 // Copyright (2002) Sandia Corporation 00009 // 00010 // LOCA: Library of Continuation Algorithms Package 00011 // Copyright (2005) Sandia Corporation 00012 // 00013 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00014 // license for use of this work by or on behalf of the U.S. Government. 00015 // 00016 // This library is free software; you can redistribute it and/or modify 00017 // it under the terms of the GNU Lesser General Public License as 00018 // published by the Free Software Foundation; either version 2.1 of the 00019 // License, or (at your option) any later version. 00020 // 00021 // This library is distributed in the hope that it will be useful, but 00022 // WITHOUT ANY WARRANTY; without even the implied warranty of 00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00024 // Lesser General Public License for more details. 00025 // 00026 // You should have received a copy of the GNU Lesser General Public 00027 // License along with this library; if not, write to the Free Software 00028 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00029 // USA 00030 // 00031 // Questions? Contact Roger Pawlowski (rppawlo@sandia.gov) or 00032 // Eric Phipps (etphipp@sandia.gov), Sandia National Laboratories. 00033 // ************************************************************************ 00034 // CVS Information 00035 // $Source: /space/CVS/Trilinos/packages/nox/src/NOX_Random.C,v $ 00036 // $Author: rppawlo $ 00037 // $Date: 2006/08/22 00:01:27 $ 00038 // $Revision: 1.7 $ 00039 // ************************************************************************ 00040 //@HEADER 00041 00042 #include "NOX_Random.H" 00043 00044 // instantiate static data 00045 double NOX::Random::seed = 1.0; 00046 00047 NOX::Random::Random() 00048 { 00049 seed = static_cast<double>(rand()); 00050 00051 // rand() can return 0 or 2147483647, so adjust seed if that happens 00052 if ((seed == 0.0) || (seed == 2147483647.0)) 00053 seed = 1.0; 00054 } 00055 00056 NOX::Random::Random(int s) 00057 { 00058 setSeed(s); 00059 } 00060 00061 void NOX::Random::setSeed(int s) 00062 { 00063 int ss = checkSeed("setSeed", s); 00064 srand(ss); 00065 seed = static_cast<double>(s); 00066 } 00067 00068 double NOX::Random::number() 00069 { 00070 const double a = 16807.0; 00071 const double bigInt = 2147483647.0; 00072 00073 seed = fmod(a*seed, bigInt); 00074 return 2.0*(seed/bigInt)-1.0; 00075 } 00076 00077 int NOX::Random::checkSeed(const string& func, int s) 00078 { 00079 if ((s < 1) || (s > 2147483646)) { 00080 cerr << "Error in NOX::Random::" << s << "(): " << "supplied seed " 00081 << s << " is not an integer between 1 and 2147483646." << endl 00082 << "Using a seed of 1 instead." << endl; 00083 return 1; 00084 } 00085 else 00086 return s; 00087 }