NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
Random/bernoulli.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <random>
32
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/NdArray.hpp"
37
38namespace nc::random
39{
40 namespace detail
41 {
42 //============================================================================
43 // Method Description:
50 template<typename GeneratorType = std::mt19937>
51 bool bernoulli(GeneratorType& generator, double inP = 0.5)
52 {
53 if (inP < 0 || inP > 1)
54 {
55 THROW_INVALID_ARGUMENT_ERROR("input probability of success must be of the range [0, 1].");
56 }
57
58 std::bernoulli_distribution dist(inP);
59 return dist(generator);
60 }
61
62 //============================================================================
63 // Method Description:
72 template<typename GeneratorType = std::mt19937>
73 NdArray<bool> bernoulli(GeneratorType& generator, const Shape& inShape, double inP = 0.5)
74 {
75 if (inP < 0 || inP > 1)
76 {
77 THROW_INVALID_ARGUMENT_ERROR("input probability of success must be of the range [0, 1].");
78 }
79
80 NdArray<bool> returnArray(inShape);
81
82 std::bernoulli_distribution dist(inP);
83
84 std::for_each(returnArray.begin(),
85 returnArray.end(),
86 [&generator, &dist](bool& value) -> void { value = dist(generator); });
87
88 return returnArray;
89 }
90 } // namespace detail
91
92 //============================================================================
93 // Method Description:
99 inline bool bernoulli(double inP = 0.5)
100 {
101 return detail::bernoulli(generator_, inP);
102 }
103
104 //============================================================================
105 // Method Description:
113 inline NdArray<bool> bernoulli(const Shape& inShape, double inP = 0.5)
114 {
115 return detail::bernoulli(generator_, inShape, inP);
116 }
117} // 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:135
iterator end() noexcept
Definition NdArrayCore.hpp:1619
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Random/bernoulli.hpp:41
bool bernoulli(GeneratorType &generator, double inP=0.5)
Definition Random/bernoulli.hpp:51
Definition Random/bernoulli.hpp:39
bool bernoulli(double inP=0.5)
Definition Random/bernoulli.hpp:99
std::mt19937_64 generator_
generator function
Definition generator.hpp:35