NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
bernoilli.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/NdArray.hpp"
35 
36 #include <algorithm>
37 #include <random>
38 #include <string>
39 
40 namespace nc
41 {
42  namespace random
43  {
44  //============================================================================
45  // Method Description:
51  inline bool bernoulli(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:
71  inline NdArray<bool> bernoulli(const Shape& inShape, double inP = 0.5)
72  {
73  if (inP < 0 || inP > 1)
74  {
75  THROW_INVALID_ARGUMENT_ERROR("input probability of success must be of the range [0, 1].");
76  }
77 
78  NdArray<bool> returnArray(inShape);
79 
80  std::bernoulli_distribution dist(inP);
81 
82  std::for_each(returnArray.begin(), returnArray.end(),
83  [&dist](bool& value) -> void
84  {
85  value = dist(generator_);
86  });
87 
88  return returnArray;
89  }
90  } // namespace random
91 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
iterator end() noexcept
Definition: NdArrayCore.hpp:1474
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
bool bernoulli(double inP=0.5)
Definition: bernoilli.hpp:51
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
Definition: Coordinate.hpp:45