Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_RandomImp.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <cmath>
11 #include <cstdlib>
12 #include <iostream>
13 #include <cstdlib>
14 
15 template <typename ScalarT>
17 Random() :
18  a(0.0),
19  b(1.0),
20  seed(static_cast<ScalarT>(rand()))
21 {
22  // rand() can return 0 or 2147483647, so adjust seed if that happens
23  if ((seed == 0.0) || (seed == 2147483647.0))
24  seed = 1.0;
25 }
26 
27 template <typename ScalarT>
29 Random(ScalarT a_, ScalarT b_) :
30  a(a_),
31  b(b_),
32  seed(static_cast<ScalarT>(rand()))
33 {
34  // rand() can return 0 or 2147483647, so adjust seed if that happens
35  if ((seed == 0.0) || (seed == 2147483647.0))
36  seed = 1.0;
37 }
38 
39 template <typename ScalarT>
41 Random(ScalarT a_, ScalarT b_, int s) :
42  a(a_),
43  b(b_),
44  seed(0.0)
45 {
46  setSeed(s);
47 }
48 
49 template <typename ScalarT>
52 {
53 }
54 
55 template <typename ScalarT>
56 void
58 setSeed(int s) {
59  int ss = checkSeed("setSeed", s);
60  srand(ss);
61  seed = static_cast<ScalarT>(s);
62 }
63 
64 template <typename ScalarT>
65 ScalarT
67 number() {
68  const ScalarT A = 16807.0;
69  const ScalarT bigInt = 2147483647.0;
70 
71  seed = std::fmod(A*seed, bigInt);
72  return (b-a)*(seed/bigInt) + a;
73 }
74 
75 template <typename ScalarT>
76 int
78 checkSeed(const std::string& func, int s) {
79  if ((s < 1) || (s > 2147483646)) {
80  std::cerr << "Error in Sacado::Random::" << s << "(): "
81  << "supplied seed "
82  << s << " is not an integer between 1 and 2147483646."
83  << std::endl << "Using a seed of 1 instead." << std::endl;
84  return 1;
85  }
86  else
87  return s;
88 }
89 
90 #ifdef HAVE_SACADO_COMPLEX
91 
92 template <typename T>
94 Random() :
95  rand_real(0.0, 1.0),
96  rand_imag(0.0, 1.0)
97 {
98 }
99 
100 template <typename T>
102 Random(const std::complex<T>& a, const std::complex<T>& b) :
103  rand_real(a.real(), b.real()),
104  rand_imag(a.imag(), b.imag())
105 {
106 }
107 
108 template <typename T>
110 Random(const std::complex<T>& a, const std::complex<T>& b, int s) :
111  rand_real(a.real(), b.real(), s),
112  rand_imag(a.imag(), b.imag(), s+1)
113 {
114 }
115 
116 template <typename T>
118 ~Random()
119 {
120 }
121 
122 template <typename T>
123 void
125 setSeed(int s) {
126  rand_real.setSeed(s);
127  rand_imag.setSeed(s+1);
128 }
129 
130 template <typename T>
131 std::complex<T>
133 number() {
134  return std::complex<T>(rand_real.number(), rand_imag.number());
135 }
136 
137 #endif // HAVE_SACADO_COMPLEX
ScalarT seed
Random number seed
int checkSeed(const std::string &func, int s)
ScalarT number()
Get random number.
~Random()
Destructor.
void setSeed(int s)
Set seed to s.
Random()
Constructor.
const T func(int n, T *x)
Definition: ad_example.cpp:29
A random number generator that generates random numbers uniformly distributed in the interval (a...