NumCpp  2.1.0
A C++ implementation of the Python Numpy library
randFloat.hpp
Go to the documentation of this file.
1 #pragma once
31 
35 #include "NumCpp/Core/Shape.hpp"
36 #include "NumCpp/NdArray.hpp"
39 
40 #include "boost/random/uniform_real_distribution.hpp"
41 
42 #include <string>
43 
44 namespace nc
45 {
46  namespace random
47  {
48  //============================================================================
49  // Method Description:
61  template<typename dtype>
62  dtype randFloat(dtype inLow, dtype inHigh = 0.0)
63  {
64  STATIC_ASSERT_FLOAT(dtype);
65 
66  if (utils::essentiallyEqual(inLow, inHigh))
67  {
68  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
69  }
70  else if (inLow > inHigh)
71  {
72  std::swap(inLow, inHigh);
73  }
74 
75  const boost::random::uniform_real_distribution<dtype> dist(inLow, inHigh - DtypeInfo<dtype>::epsilon());
76  return dist(generator_);
77  }
78 
79  //============================================================================
80  // Method Description:
93  template<typename dtype>
94  NdArray<dtype> randFloat(const Shape& inShape, dtype inLow, dtype inHigh = 0.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  const boost::random::uniform_real_distribution<dtype> dist(inLow, inHigh - DtypeInfo<dtype>::epsilon());
110 
111  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
112  [&dist](dtype& value) -> void
113  {
114  value = dist(generator_);
115  });
116 
117  return returnArray;
118  }
119  } // namespace random
120 } // namespace nc
StaticAsserts.hpp
nc::random::randFloat
dtype randFloat(dtype inLow, dtype inHigh=0.0)
Definition: randFloat.hpp:62
Error.hpp
nc::utils::essentiallyEqual
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:53
generator.hpp
nc::NdArray< dtype >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:214
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
STATIC_ASSERT_FLOAT
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:44
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1435
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:43
nc::DtypeInfo
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:40
essentiallyEqual.hpp
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091