ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-2015, 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>
45 {
46 public:
47  NumericT operator()() const { return static_cast<NumericT>(double(rand()) / double(RAND_MAX)); }
48 };
49 
50 
56 template<typename NumericT>
58 {
59 public:
60  normal_random_numbers(NumericT mean = NumericT(0), NumericT sigma = NumericT(1)) : mean_(mean), sigma_(sigma) {}
61 
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 
Random number generator for returning normally distributed values.
Definition: random.hpp:57
float NumericT
Definition: bisect.cpp:40
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
Random number generator for returning uniformly distributed values in the closed interval [0...
Definition: random.hpp:44
normal_random_numbers(NumericT mean=NumericT(0), NumericT sigma=NumericT(1))
Definition: random.hpp:60