GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-rand.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2003-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_oct_rand_h)
27#define octave_oct_rand_h 1
28
29#include "octave-config.h"
30
31#include <map>
32#include <string>
33
34#include "Array.h"
35#include "dNDArray.h"
36#include "fNDArray.h"
37#include "lo-ieee.h"
38#include "uint32NDArray.h"
39
40//class dim_vector;
41
43
45{
46protected:
47
49
50public:
51
52 OCTAVE_DISABLE_COPY_MOVE (rand)
53
54 ~rand () = default;
55
56 static bool instance_ok ();
57
58 // Return the current seed.
59 static double seed ()
60 {
61 return (instance_ok ()
62 ? s_instance->do_seed () : numeric_limits<double>::NaN ());
63 }
64
65 // Set the seed.
66 static void seed (double s)
67 {
68 if (instance_ok ())
69 s_instance->do_seed (s);
70 }
71
72 // Reset the seed.
73 static void reset ()
74 {
75 if (instance_ok ())
76 s_instance->do_reset ();
77 }
78
79 // Return the current state.
80 static uint32NDArray state (const std::string& d = "")
81 {
82 return instance_ok () ? s_instance->do_state (d) : uint32NDArray ();
83 }
84
85 // Set the current state/
86 static void state (const uint32NDArray& s,
87 const std::string& d = "")
88 {
89 if (instance_ok ())
90 s_instance->do_state (s, d);
91 }
92
93 // Reset the current state/
94 static void reset (const std::string& d)
95 {
96 if (instance_ok ())
97 s_instance->do_reset (d);
98 }
99
100 // Return the current distribution.
101 static std::string distribution ()
102 {
103 return instance_ok () ? s_instance->do_distribution () : "";
104 }
105
106 // Set the current distribution. May be either "uniform" (the
107 // default), "normal", "exponential", "poisson", or "gamma".
108 static void distribution (const std::string& d)
109 {
110 if (instance_ok ())
111 s_instance->do_distribution (d);
112 }
113
114 static void uniform_distribution ()
115 {
116 if (instance_ok ())
117 s_instance->do_uniform_distribution ();
118 }
119
120 static void normal_distribution ()
121 {
122 if (instance_ok ())
123 s_instance->do_normal_distribution ();
124 }
125
127 {
128 if (instance_ok ())
129 s_instance->do_exponential_distribution ();
130 }
131
132 static void poisson_distribution ()
133 {
134 if (instance_ok ())
135 s_instance->do_poisson_distribution ();
136 }
137
138 static void gamma_distribution ()
139 {
140 if (instance_ok ())
141 s_instance->do_gamma_distribution ();
142 }
143
144 // Return the next number from the sequence.
145 static double scalar (double a = 1.0)
146 {
147 return (instance_ok ()
148 ? s_instance->do_scalar (a) : numeric_limits<double>::NaN ());
149 }
150
151 // Return the next number from the sequence.
152 static float float_scalar (float a = 1.0)
153 {
154 return (instance_ok ()
155 ? s_instance->do_scalar (a) : numeric_limits<float>::NaN ());
156 }
157
158 // Return an array of numbers from the sequence.
159 static Array<double> vector (octave_idx_type n, double a = 1.0)
160 {
161 return instance_ok () ? s_instance->do_vector (n, a) : Array<double> ();
162 }
163
164 // Return an array of numbers from the sequence.
165 static Array<float> float_vector (octave_idx_type n, float a = 1.0)
166 {
167 return instance_ok () ? s_instance->do_vector (n, a) : Array<float> ();
168 }
169
170 // Return an N-dimensional array of numbers from the sequence,
171 // filled in column major order.
172 static NDArray nd_array (const dim_vector& dims, double a = 1.0)
173 {
174 return instance_ok () ? s_instance->do_nd_array (dims, a) : NDArray ();
175 }
176
177 // Return an N-dimensional array of numbers from the sequence,
178 // filled in column major order.
179 static FloatNDArray float_nd_array (const dim_vector& dims, float a = 1.0)
180 {
181 return (instance_ok ()
182 ? s_instance->do_float_nd_array (dims, a) : FloatNDArray ());
183 }
184
185private:
186
187 static rand *s_instance;
188
189 static void cleanup_instance ()
190 { delete s_instance; s_instance = nullptr; }
191
192 enum
193 {
194 unknown_dist,
195 uniform_dist,
196 normal_dist,
197 expon_dist,
198 poisson_dist,
199 gamma_dist
200 };
201
202 // Current distribution of random numbers.
203 int m_current_distribution;
204
205 // If TRUE, use old RANLIB generators. Otherwise, use Mersenne
206 // Twister generator.
207 bool m_use_old_generators;
208
209 // Saved MT states.
210 std::map<int, uint32NDArray> m_rand_states;
211
212 // Return the current seed.
213 OCTAVE_API double do_seed ();
214
215 // Set the seed.
216 OCTAVE_API void do_seed (double s);
217
218 // Reset the seed.
219 OCTAVE_API void do_reset ();
220
221 // Return the current state.
222 OCTAVE_API uint32NDArray do_state (const std::string& d);
223
224 // Set the current state/
225 OCTAVE_API void do_state (const uint32NDArray& s, const std::string& d);
226
227 // Reset the current state/
228 OCTAVE_API void do_reset (const std::string& d);
229
230 // Return the current distribution.
231 OCTAVE_API std::string do_distribution ();
232
233 // Set the current distribution. May be either "uniform" (the
234 // default), "normal", "exponential", "poisson", or "gamma".
235 OCTAVE_API void do_distribution (const std::string& d);
236
237 OCTAVE_API void do_uniform_distribution ();
238
239 OCTAVE_API void do_normal_distribution ();
240
241 OCTAVE_API void do_exponential_distribution ();
242
243 OCTAVE_API void do_poisson_distribution ();
244
245 OCTAVE_API void do_gamma_distribution ();
246
247 // The following templates only make sense for double and float
248 // types.
249
250 template <typename T> OCTAVE_API T uniform ();
251
252 template <typename T> OCTAVE_API T normal ();
253
254 template <typename T> OCTAVE_API T exponential ();
255
256 template <typename T> OCTAVE_API T poisson (T a);
257
258 template <typename T> OCTAVE_API T gamma (T a);
259
260 // Return the next number from the sequence.
261 template <typename T> OCTAVE_API T do_scalar (T a = 1);
262
263 // Return an array of numbers from the sequence.
264 template <typename T> OCTAVE_API Array<T>
265 do_vector (octave_idx_type n, T a = 1);
266
267 // Return an N-dimensional array of numbers from the sequence,
268 // filled in column major order.
269 OCTAVE_API NDArray do_nd_array (const dim_vector& dims, double a = 1.);
270
271 // Return an N-dimensional array of numbers from the sequence,
272 // filled in column major order.
274 do_float_nd_array (const dim_vector& dims, float a = 1.);
275
276 // Some helper functions.
277
278 OCTAVE_API void initialize_ranlib_generators ();
279
280 OCTAVE_API void initialize_mersenne_twister ();
281
282 OCTAVE_API uint32NDArray get_internal_state ();
283
284 OCTAVE_API void save_state ();
285
286 OCTAVE_API int get_dist_id (const std::string& d);
287
288 OCTAVE_API void set_internal_state (const uint32NDArray& s);
289
290 OCTAVE_API void switch_to_generator (int dist);
291
292 OCTAVE_API void fill (octave_idx_type len, double *v, double a);
293
294 OCTAVE_API void fill (octave_idx_type len, float *v, float a);
295};
296
297OCTAVE_END_NAMESPACE(octave)
298
299#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
Definition oct-rand.h:179
~rand()=default
static uint32NDArray state(const std::string &d="")
Definition oct-rand.h:80
static Array< float > float_vector(octave_idx_type n, float a=1.0)
Definition oct-rand.h:165
static void distribution(const std::string &d)
Definition oct-rand.h:108
static NDArray nd_array(const dim_vector &dims, double a=1.0)
Definition oct-rand.h:172
static void uniform_distribution()
Definition oct-rand.h:114
static double scalar(double a=1.0)
Definition oct-rand.h:145
static void seed(double s)
Definition oct-rand.h:66
static void exponential_distribution()
Definition oct-rand.h:126
static Array< double > vector(octave_idx_type n, double a=1.0)
Definition oct-rand.h:159
static float float_scalar(float a=1.0)
Definition oct-rand.h:152
static void reset(const std::string &d)
Definition oct-rand.h:94
static void reset()
Definition oct-rand.h:73
static void gamma_distribution()
Definition oct-rand.h:138
static void state(const uint32NDArray &s, const std::string &d="")
Definition oct-rand.h:86
static std::string distribution()
Definition oct-rand.h:101
static void normal_distribution()
Definition oct-rand.h:120
static double seed()
Definition oct-rand.h:59
static void poisson_distribution()
Definition oct-rand.h:132
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
function gamma(x)
Definition gamma.f:3
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition main.in.cc:55
intNDArray< octave_uint32 > uint32NDArray
F77_RET_T len
Definition xerbla.cc:61