NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
randInt.hpp
Go to the documentation of this file.
1
30#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
42namespace nc::random
43{
44 namespace detail
45 {
46 //============================================================================
47 // Method Description:
60 template<typename dtype, typename GeneratorType = std::mt19937>
61 dtype randInt(GeneratorType& generator, dtype inLow, dtype inHigh = 0)
62 {
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)
70 {
71 std::swap(inLow, inHigh);
72 }
73
74 std::uniform_int_distribution<dtype> dist(inLow, inHigh - 1);
75 return dist(generator);
76 }
77
78 //============================================================================
79 // Method Description:
93 template<typename dtype, typename GeneratorType = std::mt19937>
94 NdArray<dtype> randInt(GeneratorType& generator, const Shape& inShape, dtype inLow, dtype inHigh = 0)
95 {
97
98 if (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 - 1)
103 {
104 std::swap(inLow, inHigh);
105 }
106
107 NdArray<dtype> returnArray(inShape);
108
109 std::uniform_int_distribution<dtype> dist(inLow, inHigh - 1);
110
111 std::for_each(returnArray.begin(),
112 returnArray.end(),
113 [&dist, &generator](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 randInt(dtype inLow, dtype inHigh = 0)
134 {
135 return detail::randInt(generator_, inLow, inHigh);
136 }
137
138 //============================================================================
139 // Method Description:
152 template<typename dtype>
153 NdArray<dtype> randInt(const Shape& inShape, dtype inLow, dtype inHigh = 0)
154 {
155 return detail::randInt(generator_, inShape, inLow, inHigh);
156 }
157} // namespace nc::random
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:43
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype randInt(GeneratorType &generator, dtype inLow, dtype inHigh=0)
Definition: randInt.hpp:61
Definition: Random/bernoulli.hpp:41
dtype randInt(dtype inLow, dtype inHigh=0)
Definition: randInt.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
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42