SHOGUN  3.2.1
 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  int64_t random_s64() const
90  {
91  return random_64() & ((uint64_t(-1)<<1)>>1);
92  }
93 
94 
102  inline uint64_t random(uint64_t min_value, uint64_t max_value)
103  {
104  return min_value + random_64() % (max_value-min_value+1);
105  }
106 
114  inline int64_t random(int64_t min_value, int64_t max_value)
115  {
116  return min_value + random_s64() % (max_value-min_value+1);
117  }
118 
126  inline uint32_t random(uint32_t min_value, uint32_t max_value)
127  {
128  return min_value + random_32() % (max_value-min_value+1);
129  }
130 
138  inline int32_t random(int32_t min_value, int32_t max_value)
139  {
140  return min_value + random_s32() % (max_value-min_value+1);
141  }
142 
150  inline float32_t random(float32_t min_value, float32_t max_value)
151  {
152  return min_value + ((max_value-min_value) * random_close());
153  }
154 
162  inline float64_t random(float64_t min_value, float64_t max_value)
163  {
164  return min_value + ((max_value-min_value) * random_close());
165  }
166 
175  inline floatmax_t random(floatmax_t min_value, floatmax_t max_value)
176  {
177  return min_value + ((max_value-min_value) * random_close());
178  }
179 
186  void fill_array(uint32_t* array, int32_t size) const;
187 
194  void fill_array(uint64_t* array, int32_t size) const;
195 
203  void fill_array_oc(float64_t* array, int32_t size) const;
204 
212  void fill_array_co(float64_t* array, int32_t size) const;
213 
221  void fill_array_oo(float64_t* array, int32_t size) const;
222 
231  void fill_array_c1o2(float64_t* array, int32_t size) const;
232 
237  float64_t random_close() const;
238 
243  float64_t random_open() const;
244 
250  float64_t random_half_open() const;
251 
260  float64_t normal_distrib(float64_t mu, float64_t sigma) const;
261 
269 
275  static uint32_t generate_seed();
276 
277  virtual const char* get_name() const { return "Random"; }
278 
279  private:
281  void init();
282 
287  void reinit(uint32_t seed);
288 
294  float64_t sample_tail() const;
295 
299  float64_t GaussianPdfDenorm(float64_t x) const;
300 
304  float64_t GaussianPdfDenormInv(float64_t y) const;
305 
306  private:
308  uint32_t m_seed;
309 
311  SFMT_T* m_sfmt_32;
312 
314  SFMT_T* m_sfmt_64;
315 
317  DSFMT_T* m_dsfmt;
318 
320  int32_t m_blockCount; //= 128;
321 
323  float64_t m_R;//= 3.442619855899;
324 
326  float64_t m_A;// = 9.91256303526217e-3;
327 
329  float64_t m_uint32ToU;// = 1.0 / (float64_t)UINT32_MAX;
330 
332  float64_t m_A_div_y0;
333 
335  float64_t* m_x;
336  float64_t* m_y;
337 
342  uint32_t* m_xComp;
343 
345  CLock m_state_lock;
346  };
347 }
348 
349 #endif /* __RANDOM_H__ */

SHOGUN Machine Learning Toolbox - Documentation