NumCpp  2.3.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
randInt.hpp
Go to the documentation of this file.
1 #pragma once
31 
35 #include "NumCpp/Core/Shape.hpp"
36 #include "NumCpp/NdArray.hpp"
38 
39 #include "boost/random/uniform_int_distribution.hpp"
40 
41 #include <string>
42 
43 namespace nc
44 {
45  namespace random
46  {
47  //============================================================================
48  // Method Description:
60  template<typename dtype>
61  dtype randInt(dtype inLow, dtype inHigh = 0)
62  {
63  STATIC_ASSERT_INTEGER(dtype);
64 
65  if (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 - 1)
70  {
71  std::swap(inLow, inHigh);
72  }
73 
74  const boost::random::uniform_int_distribution<dtype> dist(inLow, inHigh - 1);
75  return dist(generator_);
76  }
77 
78  //============================================================================
79  // Method Description:
92  template<typename dtype>
93  NdArray<dtype> randInt(const Shape& inShape, dtype inLow, dtype inHigh = 0)
94  {
95  STATIC_ASSERT_INTEGER(dtype);
96 
97  if (inLow == inHigh)
98  {
99  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
100  }
101  else if (inLow > inHigh - 1)
102  {
103  std::swap(inLow, inHigh);
104  }
105 
106  NdArray<dtype> returnArray(inShape);
107 
108  const boost::random::uniform_int_distribution<dtype> dist(inLow, inHigh - 1);
109 
110  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
111  [&dist](dtype& value) -> void
112  {
113  value = dist(generator_);
114  });
115 
116  return returnArray;
117  }
118  } // namespace random
119 } // namespace nc
STATIC_ASSERT_INTEGER
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
StaticAsserts.hpp
Error.hpp
nc::random::randInt
dtype randInt(dtype inLow, dtype inHigh=0)
Definition: randInt.hpp:61
generator.hpp
nc::NdArray< dtype >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1434
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1090