NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
rand.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include <algorithm>
32 #include <random>
33 
35 #include "NumCpp/Core/Shape.hpp"
36 #include "NumCpp/NdArray.hpp"
38 
39 namespace nc
40 {
41  namespace random
42  {
43  namespace detail
44  {
45  //============================================================================
46  // Method Description:
55  template<typename dtype, typename GeneratorType = std::mt19937>
56  dtype rand(GeneratorType& generator)
57  {
58  STATIC_ASSERT_FLOAT(dtype);
59 
60  std::uniform_real_distribution<dtype> dist(static_cast<dtype>(0.0),
61  static_cast<dtype>(1.0) - DtypeInfo<dtype>::epsilon());
62  return dist(generator);
63  }
64 
65  //============================================================================
66  // Method Description:
77  template<typename dtype, typename GeneratorType = std::mt19937>
78  NdArray<dtype> rand(GeneratorType& generator, const Shape& inShape)
79  {
80  STATIC_ASSERT_FLOAT(dtype);
81 
82  NdArray<dtype> returnArray(inShape);
83 
84  std::uniform_real_distribution<dtype> dist(static_cast<dtype>(0.0),
85  static_cast<dtype>(1.0) - DtypeInfo<dtype>::epsilon());
86 
87  std::for_each(returnArray.begin(),
88  returnArray.end(),
89  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
90 
91  return returnArray;
92  }
93  } // namespace detail
94 
95  //============================================================================
96  // Method Description:
104  template<typename dtype>
105  dtype rand()
106  {
107  return detail::rand<dtype>(generator_);
108  }
109 
110  //============================================================================
111  // Method Description:
121  template<typename dtype>
122  NdArray<dtype> rand(const Shape& inShape)
123  {
124  return detail::rand<dtype>(generator_, inShape);
125  }
126  } // namespace random
127 } // namespace nc
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:45
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
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 rand(GeneratorType &generator)
Definition: rand.hpp:56
dtype rand()
Definition: rand.hpp:105
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:37
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:227
Definition: Coordinate.hpp:45