NumCpp  2.3.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
rand.hpp
Go to the documentation of this file.
1 #pragma once
30 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/NdArray.hpp"
36 
37 #include "boost/random/uniform_01.hpp"
38 
39 namespace nc
40 {
41  namespace random
42  {
43  //============================================================================
44  // Method Description:
52  template<typename dtype>
53  dtype rand()
54  {
55  STATIC_ASSERT_FLOAT(dtype);
56 
57  boost::random::uniform_01<dtype> dist;
58  return dist(generator_);
59  }
60 
61  //============================================================================
62  // Method Description:
73  template<typename dtype>
74  NdArray<dtype> rand(const Shape& inShape)
75  {
76  STATIC_ASSERT_FLOAT(dtype);
77 
78  NdArray<dtype> returnArray(inShape);
79 
80  boost::random::uniform_01<dtype> dist;
81 
82  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
83  [&dist](dtype& value) -> void
84  {
85  value = dist(generator_);
86  });
87 
88  return returnArray;
89  }
90  } // namespace random
91 } // namespace nc
StaticAsserts.hpp
generator.hpp
nc::NdArray< dtype >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
STATIC_ASSERT_FLOAT
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1434
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1090
nc::random::rand
dtype rand()
Definition: rand.hpp:53