ViennaCL - The Vienna Computing Library: viennacl/tools/random.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
random.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_RANDOM_HPP
2 #define VIENNACL_TOOLS_RANDOM_HPP
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
21 #include <ctime>
22 #include <cstdlib>
23 #include <cmath>
24
25
33 namespace viennacl
34 {
35 namespace tools
36 {
37
43 template<typename NumericT>
44 class uniform_random_numbers
45 {
46 public:
47 NumericT operator()() const { return static_cast<NumericT>(double(rand()) / double(RAND_MAX)); }
48 };
49
50
56 template<typename NumericT>
57 class normal_random_numbers
58 {
59 public:
60 normal_random_numbers(NumericT mean = NumericT(0), NumericT sigma = NumericT(1)) : mean_(mean), sigma_(sigma) {}
61
62 NumericT operator()() const
63 {
64 NumericT u = 0;
65 while (std::fabs(u) <= 0 || u >= NumericT(1))
66 u = static_cast<NumericT>(double(rand()) / double(RAND_MAX));
67
68 NumericT v = 0;
69 while (std::fabs(v) <= 0 || v >= NumericT(1))
70 v = static_cast<NumericT>(double(rand()) / double(RAND_MAX));
71
72 return mean_ + sigma_ * std::sqrt(NumericT(-2) * std::log(u)) * std::cos(NumericT(6.28318530717958647692) * v);
73 }
74
75 private:
76 NumericT mean_;
77 NumericT sigma_;
78 };
79
80 }
81 }
82
83 #endif
84
viennacl::tools::normal_random_numbers
Random number generator for returning normally distributed values.
Definition: random.hpp:57
viennacl::tools::normal_random_numbers::operator()
NumericT operator()() const
Definition: random.hpp:62
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::tools::uniform_random_numbers
Random number generator for returning uniformly distributed values in the closed interval [0...
Definition: random.hpp:44
viennacl::tools::normal_random_numbers::normal_random_numbers
normal_random_numbers(NumericT mean=NumericT(0), NumericT sigma=NumericT(1))
Definition: random.hpp:60
viennacl::tools::uniform_random_numbers::operator()
NumericT operator()() const
Definition: random.hpp:47