NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
randN.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <random>
32
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/NdArray.hpp"
37
38namespace nc::random
39{
40 namespace detail
41 {
42 //============================================================================
43 // Method Description:
52 template<typename dtype, typename GeneratorType = std::mt19937>
53 dtype randN(GeneratorType& generator)
54 {
56
57 std::normal_distribution<dtype> dist;
58 return dist(generator);
59 }
60
61 //============================================================================
62 // Method Description:
73 template<typename dtype, typename GeneratorType = std::mt19937>
74 NdArray<dtype> randN(GeneratorType& generator, const Shape& inShape)
75 {
77
78 NdArray<dtype> returnArray(inShape);
79
80 std::normal_distribution<dtype> dist;
81
82 std::for_each(returnArray.begin(),
83 returnArray.end(),
84 [&generator, &dist](dtype& value) -> void { value = dist(generator); });
85
86 return returnArray;
87 }
88 } // namespace detail
89
90 //============================================================================
91 // Method Description:
99 template<typename dtype>
100 dtype randN()
101 {
102 return detail::randN<dtype>(generator_);
103 }
104
105 //============================================================================
106 // Method Description:
116 template<typename dtype>
117 NdArray<dtype> randN(const Shape& inShape)
118 {
119 return detail::randN<dtype>(generator_, inShape);
120 }
121} // namespace nc::random
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:50
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
iterator end() noexcept
Definition: NdArrayCore.hpp:1623
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
dtype randN(GeneratorType &generator)
Definition: randN.hpp:53
Definition: Random/bernoulli.hpp:41
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:35
dtype randN()
Definition: randN.hpp:100
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225