source: mmcs/armadillo_bits/arma_rng_cxx11.hpp @ 8daa049

matrices
Last change on this file since 8daa049 was 9dd61b1, checked in by rboet <rboet@…>, 9 years ago

Avance del proyecto 60%

  • Property mode set to 100644
File size: 2.5 KB
Line 
1// Copyright (C) 2013-2015 Conrad Sanderson
2// Copyright (C) 2013-2015 NICTA (www.nicta.com.au)
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9//! \addtogroup arma_rng_cxx11
10//! @{
11
12
13#if defined(ARMA_USE_CXX11)
14
15
16class arma_rng_cxx11
17  {
18  public:
19 
20  typedef std::mt19937_64::result_type seed_type;
21 
22  inline void set_seed(const seed_type val);
23 
24  arma_inline int    randi_val();
25  arma_inline double randu_val();
26  arma_inline double randn_val();
27 
28  template<typename eT>
29  arma_inline void randn_dual_val(eT& out1, eT& out2);
30 
31  template<typename eT>
32  inline void randi_fill(eT* mem, const uword N, const int a, const int b);
33 
34  inline static int randi_max_val();
35 
36  template<typename eT>
37  inline void randg_fill(eT* mem, const uword N, const double a, const double b);
38 
39 
40  private:
41 
42  arma_aligned std::mt19937_64 engine;                           // typedef for std::mersenne_twister_engine with preset parameters
43 
44  arma_aligned std::uniform_int_distribution<int>     i_distr;   // by default uses a=0, b=std::numeric_limits<int>::max()
45 
46  arma_aligned std::uniform_real_distribution<double> u_distr;   // by default uses [0,1) interval
47 
48  arma_aligned std::normal_distribution<double>       n_distr;   // by default uses mean=0.0 and stddev=1.0
49  };
50
51
52
53inline
54void
55arma_rng_cxx11::set_seed(const arma_rng_cxx11::seed_type val)
56  {
57  engine.seed(val);
58  }
59
60
61
62arma_inline
63int
64arma_rng_cxx11::randi_val()
65  {
66  return i_distr(engine);
67  }
68
69
70
71arma_inline
72double
73arma_rng_cxx11::randu_val()
74  {
75  return u_distr(engine);
76  }
77
78
79
80arma_inline
81double
82arma_rng_cxx11::randn_val()
83  {
84  return n_distr(engine);
85  }
86
87
88
89template<typename eT>
90arma_inline
91void
92arma_rng_cxx11::randn_dual_val(eT& out1, eT& out2)
93  {
94  out1 = eT( n_distr(engine) );
95  out2 = eT( n_distr(engine) );
96  }
97
98
99
100template<typename eT>
101inline
102void
103arma_rng_cxx11::randi_fill(eT* mem, const uword N, const int a, const int b)
104  {
105  std::uniform_int_distribution<int> i_distr(a, b);
106 
107  for(uword i=0; i<N; ++i)
108    {
109    mem[i] = eT(i_distr(engine));
110    }
111  }
112
113
114
115inline
116int
117arma_rng_cxx11::randi_max_val()
118  {
119  return std::numeric_limits<int>::max();
120  }
121
122
123
124template<typename eT>
125inline
126void
127arma_rng_cxx11::randg_fill(eT* mem, const uword N, const double a, const double b)
128  {
129  std::gamma_distribution<double> g_distr(a,b);
130 
131  for(uword i=0; i<N; ++i)
132    {
133    mem[i] = eT(g_distr(engine));
134    }
135  }
136
137
138#endif
139
140
141//! @}
Note: See TracBrowser for help on using the repository browser.