NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
randN.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <algorithm>
31 #include <random>
32 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/NdArray.hpp"
37 
38 namespace nc
39 {
40  namespace random
41  {
42  namespace detail
43  {
44  //============================================================================
45  // Method Description:
54  template<typename dtype, typename GeneratorType = std::mt19937>
55  dtype randN(GeneratorType& generator)
56  {
57  STATIC_ASSERT_FLOAT(dtype);
58 
59  std::normal_distribution<dtype> dist;
60  return dist(generator);
61  }
62 
63  //============================================================================
64  // Method Description:
75  template<typename dtype, typename GeneratorType = std::mt19937>
76  NdArray<dtype> randN(GeneratorType& generator, const Shape& inShape)
77  {
78  STATIC_ASSERT_FLOAT(dtype);
79 
80  NdArray<dtype> returnArray(inShape);
81 
82  std::normal_distribution<dtype> dist;
83 
84  std::for_each(returnArray.begin(),
85  returnArray.end(),
86  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
87 
88  return returnArray;
89  }
90  } // namespace detail
91 
92  //============================================================================
93  // Method Description:
101  template<typename dtype>
102  dtype randN()
103  {
104  return detail::randN<dtype>(generator_);
105  }
106 
107  //============================================================================
108  // Method Description:
118  template<typename dtype>
119  NdArray<dtype> randN(const Shape& inShape)
120  {
121  return detail::randN<dtype>(generator_, inShape);
122  }
123  } // namespace random
124 } // namespace nc
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:45
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
iterator end() noexcept
Definition: NdArrayCore.hpp:1479
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype randN(GeneratorType &generator)
Definition: randN.hpp:55
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
dtype randN()
Definition: randN.hpp:102
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45