1
// $Id: Sacado_Random.cpp,v 1.7 2008/06/06 15:50:05 jmwille Exp $
2
// $Source: /space/CVS/Trilinos/packages/sacado/test/utils/Sacado_Random.cpp,v $
4
// ***********************************************************************
7
// Copyright (2006) Sandia Corporation
9
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10
// the U.S. Government retains certain rights in this software.
12
// This library is free software; you can redistribute it and/or modify
13
// it under the terms of the GNU Lesser General Public License as
14
// published by the Free Software Foundation; either version 2.1 of the
15
// License, or (at your option) any later version.
17
// This library is distributed in the hope that it will be useful, but
18
// WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26
// Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27
// (etphipp@sandia.gov).
29
// ***********************************************************************
36
#include "Sacado_Random.hpp"
38
Sacado::Random::Random(double a_, double b_) :
41
seed(static_cast<double>(rand()))
43
// rand() can return 0 or 2147483647, so adjust seed if that happens
44
if ((seed == 0.0) || (seed == 2147483647.0))
48
Sacado::Random::Random(double a_, double b_, int s) :
56
Sacado::Random::~Random()
61
Sacado::Random::setSeed(int s) {
62
int ss = checkSeed("setSeed", s);
64
seed = static_cast<double>(s);
68
Sacado::Random::number() {
69
const double A = 16807.0;
70
const double bigInt = 2147483647.0;
72
seed = std::fmod(A*seed, bigInt);
73
return (b-a)*(seed/bigInt) + a;
77
Sacado::Random::checkSeed(const std::string& func, int s) {
78
if ((s < 1) || (s > 2147483646)) {
79
std::cerr << "Error in Sacado::Random::" << s << "(): "
81
<< s << " is not an integer between 1 and 2147483646."
82
<< std::endl << "Using a seed of 1 instead." << std::endl;