NumCpp  2.11.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::random
39 {
40  namespace detail
41  {
42  //============================================================================
43  // Method Description:
52  template<typename dtype, typename GeneratorType = std::mt19937>
53  dtype randN(GeneratorType& generator)
54  {
55  STATIC_ASSERT_FLOAT(dtype);
56 
57  std::normal_distribution<dtype> dist;
58  return dist(generator);
59  }
60 
61  //============================================================================
62  // Method Description:
73  template<typename dtype, typename GeneratorType = std::mt19937>
74  NdArray<dtype> randN(GeneratorType& generator, const Shape& inShape)
75  {
76  STATIC_ASSERT_FLOAT(dtype);
77 
78  NdArray<dtype> returnArray(inShape);
79 
80  std::normal_distribution<dtype> dist;
81 
82  std::for_each(returnArray.begin(),
83  returnArray.end(),
84  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
85 
86  return returnArray;
87  }
88  } // namespace detail
89 
90  //============================================================================
91  // Method Description:
99  template<typename dtype>
100  dtype randN()
101  {
102  return detail::randN<dtype>(generator_);
103  }
104 
105  //============================================================================
106  // Method Description:
116  template<typename dtype>
117  NdArray<dtype> randN(const Shape& inShape)
118  {
119  return detail::randN<dtype>(generator_, inShape);
120  }
121 } // namespace nc::random
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:50
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:138
iterator end() noexcept
Definition: NdArrayCore.hpp:1576
iterator begin() noexcept
Definition: NdArrayCore.hpp:1268
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype randN(GeneratorType &generator)
Definition: randN.hpp:53
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype randN()
Definition: randN.hpp:100
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225