Sacado Package Browser (Single Doxygen Collection)
Version of the Day
Loading...
Searching...
No Matches
test
utils
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
37
template
<
typename
ScalarT>
38
Sacado::Random<ScalarT>::
39
Random
() :
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
49
template
<
typename
ScalarT>
50
Sacado::Random<ScalarT>::
51
Random
(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;
59
}
60
61
template
<
typename
ScalarT>
62
Sacado::Random<ScalarT>::
63
Random
(ScalarT a_, ScalarT b_,
int
s) :
64
a
(a_),
65
b(b_),
66
seed(0.0)
67
{
68
setSeed(s);
69
}
70
71
template
<
typename
ScalarT>
72
Sacado::Random<ScalarT>::
73
~Random
()
74
{
75
}
76
77
template
<
typename
ScalarT>
78
void
79
Sacado::Random<ScalarT>::
80
setSeed
(
int
s) {
81
int
ss = checkSeed(
"setSeed"
, s);
82
srand(ss);
83
seed =
static_cast<
ScalarT
>
(s);
84
}
85
86
template
<
typename
ScalarT>
87
ScalarT
88
Sacado::Random<ScalarT>::
89
number
() {
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
97
template
<
typename
ScalarT>
98
int
99
Sacado::Random<ScalarT>::
100
checkSeed
(
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
114
template
<
typename
T>
115
Sacado::Random< std::complex<T>
>::
116
Random() :
117
rand_real(0.0, 1.0),
118
rand_imag(0.0, 1.0)
119
{
120
}
121
122
template
<
typename
T>
123
Sacado::Random< std::complex<T>
>::
124
Random(
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
130
template
<
typename
T>
131
Sacado::Random< std::complex<T>
>::
132
Random(
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
138
template
<
typename
T>
139
Sacado::Random< std::complex<T>
>::
140
~Random()
141
{
142
}
143
144
template
<
typename
T>
145
void
146
Sacado::Random< std::complex<T>
>::
147
setSeed(
int
s) {
148
rand_real.
setSeed
(s);
149
rand_imag.setSeed(s+1);
150
}
151
152
template
<
typename
T>
153
std::complex<T>
154
Sacado::Random< std::complex<T>
>::
155
number() {
156
return
std::complex<T>(rand_real.number(), rand_imag.number());
157
}
158
159
#endif
// HAVE_SACADO_COMPLEX
a
a
Definition
Sacado_CacheFad_Ops.hpp:426
func
const T func(int n, T *x)
Definition
ad_example.cpp:49
Sacado::Random
A random number generator that generates random numbers uniformly distributed in the interval (a,...
Definition
Sacado_Random.hpp:46
Sacado::Random::Random
Random()
Constructor.
Definition
Sacado_RandomImp.hpp:39
Sacado::Random::setSeed
void setSeed(int s)
Set seed to s.
Definition
Sacado_RandomImp.hpp:80
Sacado::Random::~Random
~Random()
Destructor.
Definition
Sacado_RandomImp.hpp:73
Sacado::Random::seed
ScalarT seed
Random number seed
Definition
Sacado_Random.hpp:81
Sacado::Random::checkSeed
int checkSeed(const std::string &func, int s)
Definition
Sacado_RandomImp.hpp:100
Sacado::Random::number
ScalarT number()
Get random number.
Definition
Sacado_RandomImp.hpp:89
Generated by
1.10.0