NumCpp  2.8.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 
32 #include <algorithm>
33 #include <random>
34 #include <string>
35 
38 #include "NumCpp/Core/Shape.hpp"
39 #include "NumCpp/NdArray.hpp"
41 
42 namespace nc
43 {
44  namespace random
45  {
46  namespace detail
47  {
48  //============================================================================
49  // Method Description:
62  template<typename dtype, typename GeneratorType = std::mt19937>
63  dtype randInt(GeneratorType& generator, dtype inLow, dtype inHigh = 0)
64  {
65  STATIC_ASSERT_INTEGER(dtype);
66 
67  if (inLow == inHigh)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
70  }
71  else if (inLow > inHigh)
72  {
73  std::swap(inLow, inHigh);
74  }
75 
76  std::uniform_int_distribution<dtype> dist(inLow, inHigh - 1);
77  return dist(generator);
78  }
79 
80  //============================================================================
81  // Method Description:
95  template<typename dtype, typename GeneratorType = std::mt19937>
96  NdArray<dtype> randInt(GeneratorType& generator, const Shape& inShape, dtype inLow, dtype inHigh = 0)
97  {
98  STATIC_ASSERT_INTEGER(dtype);
99 
100  if (inLow == inHigh)
101  {
102  THROW_INVALID_ARGUMENT_ERROR("input low value must be less than the input high value.");
103  }
104  else if (inLow > inHigh - 1)
105  {
106  std::swap(inLow, inHigh);
107  }
108 
109  NdArray<dtype> returnArray(inShape);
110 
111  std::uniform_int_distribution<dtype> dist(inLow, inHigh - 1);
112 
113  std::for_each(returnArray.begin(),
114  returnArray.end(),
115  [&dist, &generator](dtype& value) -> void { value = dist(generator); });
116 
117  return returnArray;
118  }
119  } // namespace detail
120 
121  //============================================================================
122  // Method Description:
134  template<typename dtype>
135  dtype randInt(dtype inLow, dtype inHigh = 0)
136  {
137  return detail::randInt(generator_, inLow, inHigh);
138  }
139 
140  //============================================================================
141  // Method Description:
154  template<typename dtype>
155  NdArray<dtype> randInt(const Shape& inShape, dtype inLow, dtype inHigh = 0)
156  {
157  return detail::randInt(generator_, inShape, inLow, inHigh);
158  }
159  } // namespace random
160 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:40
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 randInt(GeneratorType &generator, dtype inLow, dtype inHigh=0)
Definition: randInt.hpp:63
dtype randInt(dtype inLow, dtype inHigh=0)
Definition: randInt.hpp:135
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
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42