NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
randFloat.hpp
Go to the documentation of this file.
1 #pragma once
30 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/NdArray.hpp"
37 
38 #include <algorithm>
39 #include <random>
40 #include <string>
41 
42 namespace nc
43 {
44  namespace random
45  {
46  //============================================================================
47  // Method Description:
58  template<typename dtype>
59  dtype randFloat(dtype inLow, dtype inHigh = 0.0)
60  {
61  STATIC_ASSERT_FLOAT(dtype);
62 
63  if (utils::essentiallyEqual(inLow, inHigh))
64  {
65  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
66  }
67  else if (inLow > inHigh)
68  {
69  std::swap(inLow, inHigh);
70  }
71 
72  std::uniform_real_distribution<dtype> dist(inLow, inHigh - DtypeInfo<dtype>::epsilon());
73  return dist(generator_);
74  }
75 
76  //============================================================================
77  // Method Description:
89  template<typename dtype>
90  NdArray<dtype> randFloat(const Shape& inShape, dtype inLow, dtype inHigh = 0.0)
91  {
92  STATIC_ASSERT_FLOAT(dtype);
93 
94  if (utils::essentiallyEqual(inLow, inHigh))
95  {
96  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
97  }
98  else if (inLow > inHigh)
99  {
100  std::swap(inLow, inHigh);
101  }
102 
103  NdArray<dtype> returnArray(inShape);
104 
105  std::uniform_real_distribution<dtype> dist(inLow, inHigh - DtypeInfo<dtype>::epsilon());
106 
107  std::for_each(returnArray.begin(), returnArray.end(),
108  [&dist](dtype& value) -> void
109  {
110  value = dist(generator_);
111  });
112 
113  return returnArray;
114  }
115  } // namespace random
116 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
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:1474
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype randFloat(dtype inLow, dtype inHigh=0.0)
Definition: randFloat.hpp:59
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:52
Definition: Coordinate.hpp:45
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42