NumCpp  2.13.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
choice.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31
32#include "NumCpp/Core/Enums.hpp"
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
39
40namespace nc::random
41{
42 namespace detail
43 {
44 //============================================================================
45 // Method Description:
52 template<typename dtype, typename GeneratorType = std::mt19937>
54 {
55 uint32 randIdx = detail::randInt<uint32>(generator, inArray.size());
56 return inArray[randIdx];
57 }
58
59 //============================================================================
60 // Method Description:
69 template<typename dtype, typename GeneratorType = std::mt19937>
74 {
75 if (replace == Replace::NO && inNum > inArray.size())
76 {
77 THROW_INVALID_ARGUMENT_ERROR("when Replace::NO 'inNum' must be <= inArray.size()");
78 }
79
80 if (replace == Replace::YES)
81 {
83 std::for_each(outArray.begin(),
84 outArray.end(),
85 [&generator, &inArray](dtype& value) -> void { value = choice(generator, inArray); });
86
87 return outArray;
88 }
89
91 }
92 } // namespace detail
93
94 //============================================================================
95 // Method Description:
101 template<typename dtype>
106
107 //============================================================================
108 // Method Description:
116 template<typename dtype>
121} // namespace nc::random
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
A Class for slicing into NdArrays.
Definition Slice.hpp:45
dtype choice(GeneratorType &generator, const NdArray< dtype > &inArray)
Definition choice.hpp:53
NdArray< dtype > permutation(GeneratorType &generator, dtype inValue)
Definition permutation.hpp:52
Definition Random/bernoulli.hpp:41
std::mt19937_64 generator_
generator function
Definition generator.hpp:35
dtype choice(const NdArray< dtype > &inArray)
Definition choice.hpp:102
Replace
Replace boolean.
Definition Enums.hpp:101
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
NdArray< dtype > replace(const NdArray< dtype > &inArray, dtype oldValue, dtype newValue)
Definition replace.hpp:45
std::uint32_t uint32
Definition Types.hpp:40