NumCpp  2.11.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 
31 #include <algorithm>
32 #include <random>
33 #include <string>
34 
37 #include "NumCpp/Core/Shape.hpp"
38 #include "NumCpp/NdArray.hpp"
41 
42 namespace nc::random
43 {
44  namespace detail
45  {
46  //============================================================================
47  // Method Description:
60  template<typename dtype, typename GeneratorType = std::mt19937>
61  dtype randFloat(GeneratorType& generator, dtype inLow, dtype inHigh = 0.)
62  {
63  STATIC_ASSERT_FLOAT(dtype);
64 
65  if (utils::essentiallyEqual(inLow, inHigh))
66  {
67  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
68  }
69  else if (inLow > inHigh)
70  {
71  std::swap(inLow, inHigh);
72  }
73 
74  std::uniform_real_distribution<dtype> dist(inLow, inHigh - DtypeInfo<dtype>::epsilon());
75  return dist(generator);
76  }
77 
78  //============================================================================
79  // Method Description:
93  template<typename dtype, typename GeneratorType = std::mt19937>
94  NdArray<dtype> randFloat(GeneratorType& generator, const Shape& inShape, dtype inLow, dtype inHigh = 0.)
95  {
96  STATIC_ASSERT_FLOAT(dtype);
97 
98  if (utils::essentiallyEqual(inLow, inHigh))
99  {
100  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
101  }
102  else if (inLow > inHigh)
103  {
104  std::swap(inLow, inHigh);
105  }
106 
107  NdArray<dtype> returnArray(inShape);
108 
109  std::uniform_real_distribution<dtype> dist(inLow, inHigh - DtypeInfo<dtype>::epsilon());
110 
111  std::for_each(returnArray.begin(),
112  returnArray.end(),
113  [&generator, &dist](dtype& value) -> void { value = dist(generator); });
114 
115  return returnArray;
116  }
117  } // namespace detail
118 
119  //============================================================================
120  // Method Description:
132  template<typename dtype>
133  dtype randFloat(dtype inLow, dtype inHigh = 0.)
134  {
135  return detail::randFloat(generator_, inLow, inHigh);
136  }
137 
138  //============================================================================
139  // Method Description:
152  template<typename dtype>
153  NdArray<dtype> randFloat(const Shape& inShape, dtype inLow, dtype inHigh = 0.)
154  {
155  return detail::randFloat(generator_, inShape, inLow, inHigh);
156  }
157 } // namespace nc::random
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:50
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: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 randFloat(GeneratorType &generator, dtype inLow, dtype inHigh=0.)
Definition: randFloat.hpp:61
Definition: Random/bernoulli.hpp:41
dtype randFloat(dtype inLow, dtype inHigh=0.)
Definition: randFloat.hpp:133
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:48
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42