SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Random.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Viktor Gal
8  * Copyright (C) 2013 Viktor Gal
9  */
10 
11 #ifndef __RANDOM_H__
12 #define __RANDOM_H__
13 
14 #include <shogun/base/SGObject.h>
15 #include <shogun/lib/config.h>
16 #include <shogun/lib/Lock.h>
17 #include <limits>
18 
19 /* opaque pointers */
20 struct SFMT_T;
21 struct DSFMT_T;
22 
23 namespace shogun
24 {
25  class CLock;
32  class CRandom : public CSGObject
33  {
34  public:
36  CRandom();
37 
41  CRandom(uint32_t seed);
42 
44  virtual ~CRandom();
45 
50  void set_seed(uint32_t seed);
51 
56  uint32_t get_seed() const;
57 
63  uint32_t random_32() const;
64 
70  uint64_t random_64() const;
71 
77  inline int32_t random_s32() const
78  {
79  return random_32() & ((uint32_t(-1)<<1)>>1);
80  }
81 
87  int64_t random_s64() const
88  {
89  return random_64() & ((uint64_t(-1)<<1)>>1);
90  }
91 
92 
100  inline uint64_t random(uint64_t min_value, uint64_t max_value)
101  {
102  return min_value + random_64() % (max_value-min_value+1);
103  }
104 
112  inline int64_t random(int64_t min_value, int64_t max_value)
113  {
114  return min_value + random_s64() % (max_value-min_value+1);
115  }
116 
124  inline uint32_t random(uint32_t min_value, uint32_t max_value)
125  {
126  return min_value + random_32() % (max_value-min_value+1);
127  }
128 
136  inline int32_t random(int32_t min_value, int32_t max_value)
137  {
138  return min_value + random_s32() % (max_value-min_value+1);
139  }
140 
148  inline float32_t random(float32_t min_value, float32_t max_value)
149  {
150  return min_value + ((max_value-min_value) * random_close());
151  }
152 
160  inline float64_t random(float64_t min_value, float64_t max_value)
161  {
162  return min_value + ((max_value-min_value) * random_close());
163  }
164 
173  inline floatmax_t random(floatmax_t min_value, floatmax_t max_value)
174  {
175  return min_value + ((max_value-min_value) * random_close());
176  }
177 
184  void fill_array(uint32_t* array, int32_t size) const;
185 
192  void fill_array(uint64_t* array, int32_t size) const;
193 
201  void fill_array_oc(float64_t* array, int32_t size) const;
202 
210  void fill_array_co(float64_t* array, int32_t size) const;
211 
219  void fill_array_oo(float64_t* array, int32_t size) const;
220 
229  void fill_array_c1o2(float64_t* array, int32_t size) const;
230 
235  float64_t random_close() const;
236 
241  float64_t random_open() const;
242 
248  float64_t random_half_open() const;
249 
258  float64_t normal_distrib(float64_t mu, float64_t sigma) const;
259 
267 
268 
269  virtual const char* get_name() const { return "Random"; }
270 
271  private:
273  void init();
274 
279  void reinit(uint32_t seed);
280 
286  float64_t sample_tail() const;
287 
291  float64_t GaussianPdfDenorm(float64_t x) const;
292 
296  float64_t GaussianPdfDenormInv(float64_t y) const;
297 
298  private:
300  uint32_t m_seed;
301 
303  SFMT_T* m_sfmt_32;
304 
306  SFMT_T* m_sfmt_64;
307 
309  DSFMT_T* m_dsfmt;
310 
312  int32_t m_blockCount; //= 128;
313 
315  float64_t m_R;//= 3.442619855899;
316 
318  float64_t m_A;// = 9.91256303526217e-3;
319 
321  float64_t m_uint32ToU;// = 1.0 / (float64_t)UINT32_MAX;
322 
324  float64_t m_A_div_y0;
325 
327  float64_t* m_x;
328  float64_t* m_y;
329 
334  uint32_t* m_xComp;
335 
337  CLock m_state_lock;
338  };
339 }
340 
341 #endif /* __RANDOM_H__ */

SHOGUN Machine Learning Toolbox - Documentation