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