random.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_RANDOM_H__INCLUDED
21 #define TILEDARRAY_RANDOM_H__INCLUDED
22 
23 #include <complex> // for std::complex
24 #include <cstdlib> // for std::rand
25 #include <type_traits> // for true_type, false_type, and enable_if
26 
27 namespace TiledArray::detail {
28 
29 //------------------------------------------------------------------------------
30 // CanMakeRandom
31 //------------------------------------------------------------------------------
40 template<typename ValueType>
41 struct CanMakeRandom : std::false_type{};
42 
44 template<>
45 struct CanMakeRandom<int> : std::true_type{};
46 
48 template<>
49 struct CanMakeRandom<float> : std::true_type{};
50 
52 template<>
53 struct CanMakeRandom<double> : std::true_type{};
54 
56 template<>
57 struct CanMakeRandom<std::complex<float>> : std::true_type{};
58 
60 template<>
61 struct CanMakeRandom<std::complex<double>> : std::true_type{};
62 
70 template<typename ValueType>
71 static constexpr auto can_make_random_v = CanMakeRandom<ValueType>::value;
72 
76 template<typename T>
77 using enable_if_can_make_random_t = std::enable_if_t<can_make_random_v<T>>;
78 
79 //------------------------------------------------------------------------------
80 // MakeRandom
81 //------------------------------------------------------------------------------
82 
91 template<typename ValueType>
92 struct MakeRandom {
94  static ValueType generate_value() {
95  return static_cast<ValueType>(std::rand() / RAND_MAX);
96  }
97 };
98 
108 template<typename ScalarType>
109 struct MakeRandom<std::complex<ScalarType>> {
110 
112  static auto generate_value() {
113  const ScalarType real = MakeRandom<ScalarType>::generate_value();
114  const ScalarType imag = MakeRandom<ScalarType>::generate_value();
115  return std::complex<ScalarType>(real, imag);
116  }
117 };
118 
119 } // namespace TiledArray::detail
120 
121 
122 #endif
static ValueType generate_value()
Generates a random value of type ValueType.
Definition: random.h:94
std::enable_if_t< can_make_random_v< T > > enable_if_can_make_random_t
Definition: random.h:77
static auto generate_value()
Generates a random complex number.
Definition: random.h:112