NumCpp  2.5.1
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
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
iterator end() noexcept
Definition: NdArrayCore.hpp:1558
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype rand()
Definition: rand.hpp:53
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