SHOGUN  4.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/lib/config.h>
15 
16 #include <shogun/base/SGObject.h>
17 #include <shogun/lib/config.h>
18 #include <shogun/lib/Lock.h>
19 #include <limits>
20 
21 /* opaque pointers */
22 struct SFMT_T;
23 struct DSFMT_T;
24 
25 namespace shogun
26 {
27  class CLock;
34  class CRandom : public CSGObject
35  {
36  public:
38  CRandom();
39 
43  CRandom(uint32_t seed);
44 
46  virtual ~CRandom();
47 
52  void set_seed(uint32_t seed);
53 
58  uint32_t get_seed() const;
59 
65  uint32_t random_32() const;
66 
72  uint64_t random_64() const;
73 
79  inline int32_t random_s32() const
80  {
81  return random_32() & ((uint32_t(-1)<<1)>>1);
82  }
83 
89  inline int64_t random_s64() const
90  {
91  return random_64() & ((uint64_t(-1)<<1)>>1);
92  }
93 
101  inline uint64_t random(uint64_t min_value, uint64_t max_value)
102  {
103  return min_value + random_64() % (max_value-min_value+1);
104  }
105 
113  inline int64_t random(int64_t min_value, int64_t max_value)
114  {
115  return min_value + random_s64() % (max_value-min_value+1);
116  }
117 
125  inline uint32_t random(uint32_t min_value, uint32_t max_value)
126  {
127  return min_value + random_32() % (max_value-min_value+1);
128  }
129 
137  inline int32_t random(int32_t min_value, int32_t max_value)
138  {
139  return min_value + random_s32() % (max_value-min_value+1);
140  }
141 
149  inline float32_t random(float32_t min_value, float32_t max_value)
150  {
151  return min_value + ((max_value-min_value) * random_close());
152  }
153 
161  inline float64_t random(float64_t min_value, float64_t max_value)
162  {
163  return min_value + ((max_value-min_value) * random_close());
164  }
165 
174  inline floatmax_t random(floatmax_t min_value, floatmax_t max_value)
175  {
176  return min_value + ((max_value-min_value) * random_close());
177  }
178 
185  void fill_array(uint32_t* array, int32_t size) const;
186 
193  void fill_array(uint64_t* array, int32_t size) const;
194 
202  void fill_array_oc(float64_t* array, int32_t size) const;
203 
211  void fill_array_co(float64_t* array, int32_t size) const;
212 
220  void fill_array_oo(float64_t* array, int32_t size) const;
221 
230  void fill_array_c1o2(float64_t* array, int32_t size) const;
231 
236  float64_t random_close() const;
237 
242  float64_t random_open() const;
243 
249  float64_t random_half_open() const;
250 
259  float64_t normal_distrib(float64_t mu, float64_t sigma) const;
260 
268 
274  static uint32_t generate_seed();
275 
276  virtual const char* get_name() const { return "Random"; }
277 
278  private:
280  void init();
281 
286  void reinit(uint32_t seed);
287 
293  float64_t sample_tail() const;
294 
298  float64_t GaussianPdfDenorm(float64_t x) const;
299 
303  float64_t GaussianPdfDenormInv(float64_t y) const;
304 
306  uint32_t m_seed;
307 
309  SFMT_T* m_sfmt_32;
310 
312  SFMT_T* m_sfmt_64;
313 
315  DSFMT_T* m_dsfmt;
316 
318  int32_t m_blockCount; //= 128;
319 
321  float64_t m_R;//= 3.442619855899;
322 
324  float64_t m_A;// = 9.91256303526217e-3;
325 
327  float64_t m_uint32ToU;// = 1.0 / (float64_t)UINT32_MAX;
328 
330  float64_t m_A_div_y0;
331 
333  float64_t* m_x;
334  float64_t* m_y;
335 
340  uint32_t* m_xComp;
341 
343  CLock m_state_lock;
344  };
345 }
346 
347 #endif /* __RANDOM_H__ */

SHOGUN Machine Learning Toolbox - Documentation