NumCpp  2.4.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 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/NdArray.hpp"
35 
36 #include <algorithm>
37 #include <random>
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  std::uniform_real_distribution<dtype> dist(static_cast<dtype>(0.0),
58  static_cast<dtype>(1.0) - DtypeInfo<dtype>::epsilon());
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  std::uniform_real_distribution<dtype> dist(static_cast<dtype>(0.0),
82  static_cast<dtype>(1.0) - DtypeInfo<dtype>::epsilon());
83 
84  std::for_each(returnArray.begin(), returnArray.end(),
85  [&dist](dtype& value) -> void
86  {
87  value = dist(generator_);
88  });
89 
90  return returnArray;
91  }
92  } // namespace random
93 } // 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:1431
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::DtypeInfo
Holds info about the dtype.
Definition: DtypeInfo.hpp:40
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:39
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1087
nc::random::rand
dtype rand()
Definition: rand.hpp:53