Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Sacado_RandomImp.hpp
Go to the documentation of this file.
1// $Id$
2// $Source$
3// @HEADER
4// ***********************************************************************
5//
6// Sacado Package
7// Copyright (2006) Sandia Corporation
8//
9// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10// the U.S. Government retains certain rights in this software.
11//
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.
16//
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.
21//
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25// USA
26// Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27// (etphipp@sandia.gov).
28//
29// ***********************************************************************
30// @HEADER
31
32#include <cmath>
33#include <cstdlib>
34#include <iostream>
35#include <cstdlib>
36
37template <typename ScalarT>
39Random() :
40 a(0.0),
41 b(1.0),
42 seed(static_cast<ScalarT>(rand()))
43{
44 // rand() can return 0 or 2147483647, so adjust seed if that happens
45 if ((seed == 0.0) || (seed == 2147483647.0))
46 seed = 1.0;
47}
48
49template <typename ScalarT>
51Random(ScalarT a_, ScalarT b_) :
52 a(a_),
53 b(b_),
54 seed(static_cast<ScalarT>(rand()))
55{
56 // rand() can return 0 or 2147483647, so adjust seed if that happens
57 if ((seed == 0.0) || (seed == 2147483647.0))
58 seed = 1.0;
60
61template <typename ScalarT>
63Random(ScalarT a_, ScalarT b_, int s) :
64 a(a_),
65 b(b_),
66 seed(0.0)
67{
68 setSeed(s);
69}
71template <typename ScalarT>
76
77template <typename ScalarT>
78void
80setSeed(int s) {
81 int ss = checkSeed("setSeed", s);
82 srand(ss);
83 seed = static_cast<ScalarT>(s);
84}
85
86template <typename ScalarT>
87ScalarT
89number() {
90 const ScalarT A = 16807.0;
91 const ScalarT bigInt = 2147483647.0;
92
93 seed = std::fmod(A*seed, bigInt);
94 return (b-a)*(seed/bigInt) + a;
95}
96
97template <typename ScalarT>
98int
100checkSeed(const std::string& func, int s) {
101 if ((s < 1) || (s > 2147483646)) {
102 std::cerr << "Error in Sacado::Random::" << s << "(): "
103 << "supplied seed "
104 << s << " is not an integer between 1 and 2147483646."
105 << std::endl << "Using a seed of 1 instead." << std::endl;
106 return 1;
107 }
108 else
109 return s;
110}
111
112#ifdef HAVE_SACADO_COMPLEX
113
114template <typename T>
116Random() :
117 rand_real(0.0, 1.0),
118 rand_imag(0.0, 1.0)
119{
120}
121
122template <typename T>
124Random(const std::complex<T>& a, const std::complex<T>& b) :
125 rand_real(a.real(), b.real()),
126 rand_imag(a.imag(), b.imag())
127{
128}
129
130template <typename T>
132Random(const std::complex<T>& a, const std::complex<T>& b, int s) :
133 rand_real(a.real(), b.real(), s),
134 rand_imag(a.imag(), b.imag(), s+1)
135{
136}
137
138template <typename T>
140~Random()
141{
142}
143
144template <typename T>
145void
147setSeed(int s) {
148 rand_real.setSeed(s);
149 rand_imag.setSeed(s+1);
150}
151
152template <typename T>
153std::complex<T>
155number() {
156 return std::complex<T>(rand_real.number(), rand_imag.number());
157}
158
159#endif // HAVE_SACADO_COMPLEX
const T func(int n, T *x)
A random number generator that generates random numbers uniformly distributed in the interval (a,...
Random()
Constructor.
void setSeed(int s)
Set seed to s.
~Random()
Destructor.
ScalarT seed
Random number seed
int checkSeed(const std::string &func, int s)
ScalarT number()
Get random number.