NumCpp  2.7.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:
51  template<typename dtype>
52  dtype rand()
53  {
54  STATIC_ASSERT_FLOAT(dtype);
55 
56  std::uniform_real_distribution<dtype> dist(static_cast<dtype>(0.0),
57  static_cast<dtype>(1.0) - DtypeInfo<dtype>::epsilon());
58  return dist(generator_);
59  }
60 
61  //============================================================================
62  // Method Description:
71  template<typename dtype>
72  NdArray<dtype> rand(const Shape& inShape)
73  {
74  STATIC_ASSERT_FLOAT(dtype);
75 
76  NdArray<dtype> returnArray(inShape);
77 
78  std::uniform_real_distribution<dtype> dist(static_cast<dtype>(0.0),
79  static_cast<dtype>(1.0) - DtypeInfo<dtype>::epsilon());
80 
81  std::for_each(returnArray.begin(), returnArray.end(),
82  [&dist](dtype& value) -> void
83  {
84  value = dist(generator_);
85  });
86 
87  return returnArray;
88  }
89  } // namespace random
90 } // 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:1474
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype rand()
Definition: rand.hpp:52
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